From my basic knowledge, I'd expect a socket representing a TCP/IP connection to guarantee delivery of the requested data. So, is there ever a case that the return value of send()
or write()
would be less than the number of bytes requested ?
Neither send
nor write
guarantee, that all the submitted data are actually written to the socket. Instead they return how many data are written - and only for these actually written data the reliability guarantees of TCP apply.
Instead of blindly relying on send
or write
to write everything the caller MUST check the return value of the function to find out how many data are written and submit the remaining data later with another call.
The technical explanation for this is that these functions don't actually directly write to the peer. Instead the data gets submitted to the write buffer to the socket which has a limited size. If the buffer is full then send
and write
will block (unless the socket is set non-blocking) until the OS has taken data out of the write buffer for sending it to the peer. Otherwise these functions will append data to the send buffer, but only up to the maximum size of the buffer. Since the submitted data might exceed the available space in the buffer the function might write less than given and the caller must deal with this situation by checking the return code.