Hi,
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