Is it ever possible for the C send
function to return zero when using TCP sockets? The man page just says that it will return the number of bytes sent, but I am not sure if it will just return -1 when it can't send any data.
views:
210answers:
3Well, there is always the case where you passed in zero as the number of bytes to send... in that case, "returning the number of bytes sent" would indicate that it should return zero bytes.
Probably best to handle the returns-zero case properly anyway; it can't hurt, and it might help.
The answer to this may well be implementation dependent and therefore vary based on the operating system.
One circumstance where 0 would be expected, when you request a transmission of 0 bytes.
Yes, it can indeed return zero. I've seen this in the situation of massive data transfers where the other end is not keeping up.
In that case, the remote TCP stack buffers had filled up, the stack had notified the local end that it was to delay until some space was cleared out and the local buffers had filled up as well.
At that point, it's not technically an error (hence no -1 returned) but no data could be accepted by the local stack.
This flow control is a basic feature of TCP. Receivers send back a window size with each acknowledgement indicating how much data they can accept. Once this hits zero, the sender no longer transmits until told that it's okay. Note that this is the TCP stack doing this, al the application sees (eventually) is a zero return code from the send.
Once the window size has been zero for a while (by using a persist timer on the sender), then an error may be generated.
So it returns zero. Granted, it's an edge case but you always code for edge cases.