I must be running into a buffer limit somewhere receiving data from TCP socket using recv( )

StackOverflow https://stackoverflow.com/questions/3591692

  •  01-10-2019
  •  | 
  •  

Question

I'm downloading 6kB of data from a test instrument connected via TCP socket. I'm using SOCK_STREAM:

if((MySocket=socket(PF_INET,SOCK_STREAM,0))==-1) exit(1);

I haven't set the any buffer sizes, so I assume I'm using whatever the default is. I'm having a problem with receiving data. Here's the statement:

  if((recv(MySocket, &YRaw[indx], sizeChunk*2, 0))==-1)
     {perror("recv()"); exit(9); }  // where sizeChunk=3000; sizeof(YRaw[0])=2

where YRaw is a 3000 object array, each object is 2 bytes.

The problem I observe after running the code, is that about 3/4 of the way through YRaw array, there's a string of zeros, which are what I initialized the array to. It appears the receive function works fine up to 3/4 of the array, then skips a chunk of YRaw array (leaving the initialized values), then picks up again, and the YRaw array fills up. There's data remaining in the buffer to read, equal to the number of values skipped in the YRaw array.

I suspect this is related to some buffer in the system. Can anyone shed some light on how to fix this?

I don't know if it's related, but I also set TCP_NODELAY, shown here:

// TCP_NODELAY option minimizes latency by instructing system to send small msgs immediately
// Insert setsockopt() before connect() occurs
int StateNODELAY = 1; // turn NODELAY on
if(setsockopt(MySocket, IPPROTO_TCP,TCP_NODELAY,
   (void *) &StateNODELAY, sizeof StateNODELAY)==-1) {perror("setsockopt()"); exit(2); };

Best regards, gkk

Was it helpful?

Solution

recv() doesn't necessarily fill the whole buffer. It just reads whatever is available and returns how many bytes have been read.

So inspect the return value to see how much data was available, process that data, and call recv() again until you received all the data you expect.

OTHER TIPS

You should definitively store the result of recv (i.e. not only check whether it's -1 or not). That's the size of bytes actually read and you need this to know how many bytes are valid in your buffer.

Apart from that, you should check if you can read more bytes without blocking and read more until it would block.

Buffer size limitations can be anywhere in the system, e.g. the Ethernet MTU or even on your sending device.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top