Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does recv(...) operate this way?

I'm setting a timeout for the socket using SO_RCVTIMEO to 10 seconds. This question is specific to a stream socket (TCP). When I call recv(...) from what I can gather from the man pages, here is what I'm expecting:

  1. If remote closes the connection, it returns 0 immediately regardless of the timeout.
  2. If the timeout expires and no data was received, then we get a -1 returned and an errno of EAGAIN or EWOULDBLOCK.
  3. If an error occurs on the socket, it returns immediately with a -1 and then errno is set properly.
  4. If data becomes available, the socket waits until the timeout occurs before returning. This time it'll return in 10 seconds with the total bytes received.

Is this the correct behavior? I just want to make sure I'm properly understanding the docs.

Thanks! Brett

like image 967
Brett Avatar asked Dec 16 '13 18:12

Brett


1 Answers

  1. Correct.
  2. There are two different specifications (unfortunately I cannot test currently)
    2.1 ETIMEOUT would be returned. (EAGAIN or EWOULDBLOCK are returned on non-blocking sockets if no data is immediately available.) http://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html
    2.2 EAGAIN or EWOULDBLOCK would be returned for both possibilities mentioned under 2.1 http://man7.org/linux/man-pages/man2/recvmsg.2.html
  3. Correct.
  4. If at least 1 byte was read the socket might return anytime, even if less was read then told and the time-out did not yet expire.
like image 101
alk Avatar answered Nov 15 '22 04:11

alk