Blocking sockets: when, exactly, does “send()” return?

后端 未结 5 1073
耶瑟儿~
耶瑟儿~ 2020-12-07 12:44

When, exactly, does the BSD socket send() function return to the caller?

In non-blocking mode, it should return immediately, correct?

A

5条回答
  •  攒了一身酷
    2020-12-07 13:09

    The send() will return as soon as the data has been accepted by the kernel. 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). It sets the errno EWOULDBLOCK or EAGAIN. This means at that time of send(), the buffer was not able to intake all the bytes and you should try again with select() call to send() the data again. Or you could put a loop with a sleep() and call send(), but you have to take care of number of bytes actually sent and the remaining number of bytes that are to be sent.

提交回复
热议问题