title: Can send() on a TCP socket return >=0 and <length?
body: <p>I've seen a number of questions regarding <code>send()</code> 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 <code>send()</code> system call as it interacts with the networking layer of the local system.</p>
  
  <p>According to the POSIX standard, and the <code>send()</code> documentation I've read, the <em>length of the message to be sent is specified by the</em> length <em>argument</em>.  Note that: <code>send()</code> sends <em>one</em> message, of length <em>length</em>.  Further:</p>
  
  <blockquote>
    <p>If space is not available at the sending socket to hold the message to
    be transmitted, and the socket file descriptor does not have
    <code>O_NONBLOCK</code> set, <code>send()</code> 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 have <code>O_NONBLOCK</code> set,
    <code>send()</code> shall fail.</p>
  </blockquote>
  
  <p>I don't see any possibility in this definition for send() to ever return any value other than <code>-1</code> (which means no data is queued in the kernel to be transmitted) or <em>length</em>, which means the entire message is queued in the kernel to be transmitted.  I.e., it seems to me that <code>send()</code> must be atomic <em>with respect to locally queuing the message for delivery in the kernel</em>.</p>
  
  <ol>
  <li>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 <em>length</em>.</li>
  <li>If a signal occurs during <code>send()</code>, then it must return <code>-1</code>.  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.</li>
  <li>If there is <em>not</em> enough room in the socket queue in the kernel for the entire message and the socket is blocking, then according to the above statement <code>send()</code> must block until space becomes available.  Then the message will be queued and <code>send()</code> returns <em>length</em>.</li>
  <li>If there is not enough room in the socket queue in the kernel for the entire message and the socket is non-blocking, then <code>send()</code> must fail (return <code>-1</code>) and <code>errno</code> will be set to <code>EAGAIN</code> or <code>EWOULDBLOCK</code>.  Again, since we return <code>-1</code> it's clear that in this situation no part of the message can be queued.</li>
  </ol>
  
  <p>Am I missing something?  Is it possible for send() to return a value which is <code>&gt;=0 &amp;&amp; &lt;length</code>?  In what situation?  What about non-POSIX/UNIX systems?  Is the Windows <code>send()</code> implementation conforming with this?</p>