I have read the documentation at least 10 times now and have also read some 10 or so code snippets and full programs where non-blocking sockets are used for sending data. The problem is that some of the tutorials are either for beginners (Beejs f.i.) or are pretty sloppy in their assumptions; and those that are not are complicated are specialized code examples that don't explain why they do what they do. Even SO knowledge base doesn't exhaustively cover the entire gamut of send
behavior, in my opinion. What I am after are details on f.e:
- What does return code of 0 indicate exactly, and is it worth checking
errno
then or should one just discard the connection without further investigation? - Does getting a negative return value warrant closing a connection gone bad, or is it only so unless
errno
isEWOULDBLOCK
,EAGAIN
orEINTR
(...others) ? - Is it worth checking
errno
when return value is> 0
? Apparently, the value indicates amount of data "sent" (in quotes because it's a long process really, right), but since the socket is non-blocking, does it mean one can issue another call right away, or, depending onerrno
again, one should wait for the next sending occasion (using select/poll/epoll) ? - Basically, does one check the return value first and only then the
errno
value? Or maybesend
setserrno
on each call, return value regardless? That would make error checking somewhat easier... - If one gets
EINTR
, what would be a good, robust behavior for a program to take? Simply record the state and retry on next send occasion, like withEWOULDBLOCK
andEAGAIN
? - Does one check for both
EWOULDBLOCK
andEAGAIN
? Can we trust both having the same value, or does it depend on the implementation? - Does
send
returnEMSGSIZE
for stream sockets? If it doesn't, then no buffer size is too big, right? - Can return value itself be equal to either of the known error codes?
If you could provide an example of robust non-blocking send code, it would be absolutely appreciated.
len
. For a datagram protocol like UDP, this would even result in a zero-byte packet being sent. Also, errno is not guaranteed to be unchanged across a successful library call. – Larsen