Article View: borland.public.cppbuilder.internet.socket
Article #4177Re: TClientSocket ReceiveLength() returns large value
From: Gary Johnson
Date: Wed, 16 Mar 2005 22:54
Date: Wed, 16 Mar 2005 22:54
107 lines
5071 bytes
5071 bytes
ok. so i guess that means not all of the post on that site are correct, so lets try another one: Comment from site: //ReceiveLength() is not guaranteed to tell you the exact number of bytes ready. Better to use something like... //char charbuffer[128]; //int bytesRead = Socket->ReceiveBuf(charbuffer, sizeof(charbuffer)); // Now examine bytesRead to figure out how many were actually read. // Note that bytesRead will not be more than 128. Also note that // I used sizeof(charbuffer) here, when I told you that the previous // code wouldn't work with it. Notice the difference in the charbuffer // definitions. Earlier it was a char*. Here it is an array of 128 chars. Does this mean that i should give ReceiveBuf() the size of my buffer and just take what it gives me? Does this further mean that i don't really need to use ReceiveLength() at all? //------------------------------- "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:4238735c$1@newsgroups.borland.com... > > "Gary Johnson" <gjohnson@cinele.com> wrote in message > news:42385392$3@newsgroups.borland.com... > > > i grabbed the code below in a post you suggested. this is the > > method i used originally, and it had the same result. > > Then you did not use it properly to begin with. > > > the data being transmitted by the server (as being detected by > > a network analyzer) was always in packets less than or equal > > to 1514 bytes. > > If you only call ReceiveLength() once for each triggering of the event, and > it gives you a value greater than 0, then that is how many bytes are > actually available on the socket for reading. You must read that exact > amount from the socket at that moment. No more, no less. Otherwise the > reading will become out of synch over time. > > > you said "Only process packets when your buffer contains full > > packets." i don't know what that means. > > I mean, you are expecting your packets to be a certain size each time. > Whenever you read incoming data and buffer it, if the buffer does not > contain enough data to constitute a full packet, then you do not process the > data. You keep reading more data from the socket and adding it to the > buffer. Only when the buffer contains enough data for a full packet, then > you process the data for as many full packets as are available, and then > remove them from the buffer, keeping the remaining data in the buffer for > later processing. > > > how do i know whether or not i have received a full packet? > > That is your own responsibility to keep track of. Only you know what kind > of data you are actually receiving, so only you know how large each packet > is supposed to be. > > > particularly in my problem case in which i have received > > something larger than 1514. > > The size does not matter. If ReceiveLength() tells you that many bytes are > available, than that many bytes really are available, and you must read them > all. My guess would be that the server is sending you multiple packets and > your code is receiving a single event for them. So, you buffer all of them > in one go, and then process as many full packets from the buffer as you can. > > > int n_received = Socket->ReceiveBuf ( buf, > receivelength ); > > > > > > //and n_received == receivelength always! > > Of course it is. It is supposed to be. The whole point of ReceiveLength() > is to specify how many bytes can be read in a single operation without > blocking the socket. If your receiving buffer is large enough, then the > full data for that event can be read in a sincle call to ReceiveBuf(). If > the receiving buffer is not large enough, then you have to call ReceiveBuf() > multiple times, reading no more than the total number of bytes specified by > ReceiveLength(). > > > Gambit > >
Technical Details
Message-ID:
<4238b921@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4235f30d@newsgroups.borland.com> <4235f764$2@newsgroups.borland.com> <42375d21$1@newsgroups.borland.com> <423769e3$1@newsgroups.borland.com> <42385392$3@newsgroups.borland.com> <4238735c$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,d813a4525d839272 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Gary Johnson" <gjohnson@cinele.com> From: "Gary Johnson" <gjohnson@cinele.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <4235f30d@newsgroups.borland.com> <4235f764$2@newsgroups.borland.com> <42375d21$1@newsgroups.borland.com> <423769e3$1@newsgroups.borland.com> <42385392$3@newsgroups.borland.com> <4238735c$1@newsgroups.borland.com> Subject: Re: TClientSocket ReceiveLength() returns large value Date: Wed, 16 Mar 2005 17:54:24 -0500 Lines: 109 Organization: Cincinnati Electronics X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 66.117.195.80 Message-ID: <4238b921@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1111013665 66.117.195.80 (16 Mar 2005 14:54:25 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail