Two cases are well-documented in the man pages for non-blocking sockets:
What's not documented for nonblocking sockets is:
Is it safe to assume that the send() would return EAGAIN/EWOULDBLOCK on even one more byte of data? Or should a non-blocking program try to send() one more time to get a conclusive EAGAIN/EWOULDBLOCK? I'm worried about putting an EPOLLOUT watcher on the socket if it's not actually in a "would block" state to respond to it coming out of.
Obviously, the latter strategy (trying again to get something conclusive) has well-defined behavior, but it's more verbose and puts a hit on performance.
A call to send
has three possible outcomes:
send
succeeds and returns the number of bytes accepted (possibly fewer than you asked for).send
.send
blockssend
fails with EWOULDBLOCK
/EAGAIN
send
fails with another errorIf the number of bytes accepted by send
is smaller than the amount you asked for, then this consequently means that the send buffer is now completely full. However, this is purely circumstantial and non-authorative in respect of any future calls to send
.
The information returned by send
is merely a "snapshot" of the current state at the time you called send
. By the time send
has returned or by the time you call send
again, this information may already be outdated. The network card might put a datagram on the wire while your program is inside send
, or a nanosecond later, or at any other time -- there is no way of knowing. You'll know when the next call succeeds (or when it doesn't).
In other words, this does not imply that the next call to send
will return EWOULDBLOCK
/EAGAIN
(or would block if the socket wasn't non-blocking). Trying until what you called "getting a conclusive EWOULDBLOCK
" is the correct thing to do.