Search code examples
linuxfunctionrecv

linux recv() function flag parameter?


What will happen if I use recv(sockfd, buffer, len, 0); on a non-blocking socket?

If the socket sockfd is closed or nothing to be read, does the recv() block? (note: the flag in recv () is 0).


Solution

  • If the socket is marked non blocking, recv will never block. period.

    If the socket is fine but there is no data to be read you will get -1 as return value and errno will be set to EAGAIN.

    If there is an error (closed socket etc.), you still get a -1 return value but errno will be set to the appropriate value.