Re: TCP takes a long time to send packet
- From: "David Schwartz" <davids@xxxxxxxxxxxxx>
- Date: Sun, 15 Jan 2006 15:34:26 -0800
<kvartan@xxxxxxxxx> wrote in message
news:1137341298.116071.274980@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi,
> I am working on a TCP socket and I am facing this very weird problem.
> Here is some psudo code
> /*** client ***/
> // assume everything setup fine, and socket connects
> // sendBuf = constant string of length 53 bytes.
> for(int i = 0; i<100; i++){
> send(sock, sendBuf, sendBufLength, 0);
> }
> sendBuf = "end of code";
> send(sock, sendBuf, strlen(sendBuf), 0);
That's awful. You need to send the data in larger chunks. Too many
system calls, no way for the TCP stack to know what's coming.
>
> /** server **/
> // socket setup is done.
> while(strcmp(recvBuf, "end of code") != 0){
> recvBytes = recv(sock, recvBuf,... );
> }
This is wrong for many reasons, not the least of which is that 'strcmp'
requires a C-style string, not an arbitrary collection of data bytes. You
need something more like:
RecvSoFar=0;
recvBuf[0]='\0';
do
{
recvBytes=recv(sock, recvBuf+RecvSoFar, bufSize-RecvSoFar-1);
if(recvBytes<=0)
{
// handle error or connection close
}
RecvSoFar+=recvBytes;
recvBuf[RecvSoFar]=0; // now we have a c-style strong
} while(strcmp(recvBuf, "end of code")==NULL);
This is still an ugly way to do things, but at least it has no serious
errors (except checking if we fill up the buffer without getting the ending
string or receive a zero in the data stream).
DS
.
- Follow-Ups:
- Re: TCP takes a long time to send packet
- From: Barry Margolin
- Re: TCP takes a long time to send packet
- References:
- TCP takes a long time to send packet
- From: kvartan
- TCP takes a long time to send packet
- Prev by Date: Re: line by line tracing (gprof? not sure what to use)
- Next by Date: Re: TCP takes a long time to send packet
- Previous by thread: Re: TCP takes a long time to send packet
- Next by thread: Re: TCP takes a long time to send packet
- Index(es):
Relevant Pages
|