-
client/server
Hi,
I am currently working on a client server program and when the server sends a packet over - I have the following
while(( n = read(socket, receive_buffer, BUFLEN)) > 0)
{ receive_buffer[n] = 0;
fputs(receive_buffer, stdout);
}
return;
where BUFLEN = 100, to receive the info from the server. so this will loop through until I am done reading everything, the problem I am having is it dosen't return when it should. Any ideas what I can do?
Thanks in advance
-
> the problem I am having is it dosen't return when it should.
When should it return?
It's my understanding that if you send 100 bytes in a single call that it might arrive as follows
- a single 100 byte message
- a single byte, then 99 bytes
- 50 and 50
etc etc
You need to have sufficient information at the receiver to reconstruct (in the worst case) a message a byte at a time. In the case of text strings, this means either sending the length in advance, or sending the \0 at the end.
-
Try checking for " != -1" instead of ">0" as follows:
while(( n = read(socket, receive_buffer, BUFLEN)) > 0)
try:
while(( n = read(socket, receive_buffer, BUFLEN)) != -1)
Maybe it won't solve the problem, but it might put you on the correct track.