25

The standard method to send data on a stream socket has always been to call send with a chunk of data to write, check the return value to see if all data was sent and then keep calling send again until the whole message has been accepted.

For example this is a simple example of a common scheme:

int send_all(int sock, unsigned char *buffer, int len) {
  int nsent;

  while(len > 0) {
    nsent = send(sock, buffer, len, 0);
    if(nsent == -1) // error
      return -1;

    buffer += nsent;
    len -= nsent;
  }
  return 0; // ok, all data sent
}

Even the BSD manpage mentions that

...If no messages space is available at the socket to hold the message to be transmitted, then send() normally blocks...

Which indicates that we should assume that send may return without sending all data. Now I find this rather broken but even W. Richard Stevens assumes this in his standard reference book about network programming, not in the beginning chapters, but the more advanced examples uses his own writen (write all data) function instead of calling write.

Now I consider this still to be more or less broken, since if send is not able to transmit all data or accept the data in the underlying buffer and the socket is blocking, then send should block and return when the whole send request has been accepted.

I mean, in the code example above, what will happen if send returns with less data sent is that it will be called right again with a new request. What has changed since last call? At max a few hundred CPU cycles have passed so the buffer is still full. If send now accepts the data why could'nt it accept it before?

Otherwise we will end upp with an inefficient loop where we are trying to send data on a socket that cannot accept data and keep trying, or else?

So it seems like the workaround, if needed, results in heavily inefficient code and in those circumstances blocking sockets should be avoided at all an non blocking sockets together with select should be used instead.

1
  • It isn't assumed. The only way this can happen is either an interrupt or non-blocking mode.
    – user207421
    Jan 17, 2020 at 8:43

1 Answer 1

24

The thing that is missing in above description is, in Unix, system calls might get interrupted with signals. That's exactly the reason blocking send(2) might return a short count.

8
  • 1
    Wouldn't that result in EINTR? Or will EINTR only happen if the call is interrupted before any data is transmitted as described in manpagez.com/man/2/send and if data has been sent, EINTR is not issued, and instead the amount of data sent is returned?
    – Ernelli
    Apr 11, 2010 at 21:28
  • 3
    EINTR is only returned if no data has been transferred yet (and the signal handler was not installed with the SA_RESTART flag).
    – mark4o
    Apr 12, 2010 at 15:22
  • 2
    Is that really the only reason? Would that mean that, if I don't install any signal handlers, the loop around send would be unnecessary? That would simplify things quite a bit for me. Unfortunately, documentation on the matter seems to be pretty scarce...
    – lxgr
    Jan 17, 2012 at 18:49
  • 2
    @lxgr: SO_SNDTIMEO and possibly also close()ing the fd on another thread may cause short writes. Also, 3rd party libs may do weird things with signals.
    – ninjalj
    Feb 24, 2014 at 13:27
  • 1
    Ah! I was wondering why it would block and not fulfill your request. How often do signals happen?
    – Pepijn
    Apr 10, 2014 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.