I've seen a number of questions regarding send()
that discuss the underlying protocol. I'm fully aware that for TCP any message may be broken up into parts as it's sent and there's no guarantee that the receiver will get the message in one atomic operation. In this question I'm talking solely about the behavior of the send()
system call as it interacts with the networking layer of the local system.
According to the POSIX standard, and the send()
documentation I've read, the length of the message to be sent is specified by the length argument. Note that: send()
sends one message, of length length. Further:
If space is not available at the sending socket to hold the message to be transmitted, and the socket file descriptor does not have
O_NONBLOCK
set,send()
shall block until space is available. If space is not available at the sending socket to hold the message to be transmitted, and the socket file descriptor does haveO_NONBLOCK
set,send()
shall fail.
I don't see any possibility in this definition for send() to ever return any value other than -1
(which means no data is queued in the kernel to be transmitted) or length, which means the entire message is queued in the kernel to be transmitted. I.e., it seems to me that send()
must be atomic with respect to locally queuing the message for delivery in the kernel.
send()
, then it must return -1
. Obviously we cannot have queued part of the message in this case, since we don't know how much was sent. So nothing can be sent in this situation.send()
must block until space becomes available. Then the message will be queued and send()
returns length.send()
must fail (return -1
) and errno
will be set to EAGAIN
or EWOULDBLOCK
. Again, since we return -1
it's clear that in this situation no part of the message can be queued.Am I missing something? Is it possible for send() to return a value which is >=0 && <length
? In what situation? What about non-POSIX/UNIX systems? Is the Windows send()
implementation conforming with this?
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.