Can send() on a TCP socket return >=0 and <length?
Your point 2 is over-simplified. The normal condition under which send
returns a value greater than zero but less than length (note that, as others have said, it can never return zero except possibly when the length argument is zero) is when the message is sufficiently long to cause blocking, and an interrupting signal arrives after some content has already been sent. In this case, send
cannot fail with EINTR
(because this would prevent the application from knowing it had already successfully sent some data) and it cannot re-block (since the signal is interrupting, and the whole point of that is to get out of blocking), so it has to return the number of bytes already sent, which is less than the total length requested.
I know how the thing works on Linux, with the GNU C Library. Point 4 of your question reads differently in this case. If you set the flag O_NONBLOCK
for the file descriptor, and if it is not possible to queue the entire message in the kernel atomically, send()
returns the number of bytes actually sent (it can be between 1 and length), and errno
is set to EWOULDBLOCK
.
(With a file descriptor working in the blocking mode, send()
would block.)
According to the Posix specification and all the man 2 send pages I have ever seen in 30 years, yes,
send()
can return any value > 0 and <=length
. Note that it cannot return zero.According to a discussion a few years ago on news:comp.protocols.tcp-ip where all the TCP implementors are, a blocking
send()
won't actually return until it has transferred all the data to the socket send buffer: in other words, the return value is either -1 orlength.
It was agreed that this was true of all known implementations, and also true ofwrite(),
writev()
,sendmsg()
,writev()
,