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?
The send() function sends data on the socket with descriptor socket. The send() call applies to all connected sockets. Parameter Description socket. The socket descriptor. msg.
In case of blocking socket: The send() will block if the kernel buffer is not free enough to intake the data provided to send() call. Non blocking sockets: send() will not block, but would fail and returns -1 or it may return number of bytes copied partially(depending on the buffer space available).
You can't send or receive anything until you are connected to another TCP socket on the remote machine. Once connected, a TCP socket can only send and receive to/from the remote machine. This means that you'll need one TCP socket for each client in your application.
For most socket interfaces, the maximum number of sockets allowed per each connection between an application and the TCP/IP sockets interface is 65535.
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.
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 or length.
It was agreed that this was true of all known implementations, and also true of write(),
writev()
, sendmsg()
, writev()
,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With