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.
- If there is enough room in the socket queue in the kernel for the entire message and no signal occurs (normal case), it's copied and returns length.
- If a signal occurs during
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. - If there is not enough room in the socket queue in the kernel for the entire message and the socket is blocking, then according to the above statement
send()
must block until space becomes available. Then the message will be queued andsend()
returns length. - If there is not enough room in the socket queue in the kernel for the entire message and the socket is non-blocking, then
send()
must fail (return-1
) anderrno
will be set toEAGAIN
orEWOULDBLOCK
. 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?