I'm just learning socket/network programming in C, and have run into a situation where the recv() function is resulting in some unexpected behaviour (unexpected to me at least).
I have a server program with a socket set up, and a call to recv() like so:
bytes_received = recv(connected, recv_data, 5, 0);
recv_data is a 5 byte buffer.
My client program uses send() to send data, its sending buffer is 1024 bytes, though since it is reading from standard input, I've only been giving it between 1-10 bytes during this experiment (characters).
I'm seeing that even if my recv() call uses a 5 byte buffer and imposes a limit of 5 bytes, I can still send more than 5 bytes and it seems to just queue somewhere on the server side, such that the next time recv() is called (in a loop for instance), it receives the next 5 bytes of incoming data.
My understanding was that send() would fire off its data (lets say all 12 bytes), but since recv() is only "set up" to handle 5 bytes, the final 7 would be lost. Where is it that the server is getting these from? Is there an underlying input buffer at the socket level that recv() is poping data from?
The implementation of the socket (within the kernel) handles this automatically for you.
As long as it is a TCP socket you would not lose data. If it is a UDP socket you might lose data if your buffers overflow..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With