Re: TCP takes a long time to send packet




<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


.



Relevant Pages

  • Re: TCP/IP Sockets with GNAT.Sockets
    ... > from a TCP socket in a C program we have always to code the readin a ... or with no expectation until a nonblocking socket returns ... select/poll indicates no data ready for a sufficient time (which most ... certainly provide a suitable type definition of this for you. ...
    (comp.lang.ada)
  • Re: Is it possible to detect TCP session term without reading pending data?
    ... > I have an application which should only read from a TCP socket if ... > detect if the TCP peer closed the connection from his side. ... poll() becomes useless to me because it only ...
    (comp.unix.programmer)
  • Re: whereis the socket mailing list
    ... As we know,the close behavier under tcp socket is shown as below: ... the server generates a FIN_ACK from the server, ... The nitty gritty TCP sequencing and timing is a little bit ...
    (perl.beginners)
  • TCP sockets: NO_DELAY
    ... I am new to sockets, and I am trying to write a simple TCP socket using ... force sending a packet. ... All my code is doing is sending short strings and on the host side I am ...
    (microsoft.public.win32.programmer.networks)
  • SendBuf or SendText on non-blocking socket
    ... The socket is set to be non-blocking, and input data is ... which) to SendText or SendBuf, ...
    (comp.lang.pascal.delphi.misc)