Errno 35 (EAGAIN) returned on recv call

You either set the socket to non-blocking mode or enabled the receive timeout. Here's from recv(2) on a mac:

The calls fail if:

[EAGAIN] The socket is marked non-blocking, and the receive operation would block, or a receive timeout had been set, and the timeout expired before data were received.

Edit 0:

Hmm, apologies for quoting again. This time from intro(2):

11 EDEADLK Resource deadlock avoided. An attempt was made to lock a system resource that would have resulted in a deadlock situation.

...

35 EAGAIN Resource temporarily unavailable. This is a temporary condition and later calls to the same routine may complete normally.

Just use strerror(3) to figure out the actual issue.


Your socket is in non-blocking mode. EAGAIN is the normal return from recv() (and other system calls) when there is no data available to read. In that sense it's not really an error.

If you meant for your socket to be nonblocking then you need to monitor it to find out when it has data available and only call recv() when there is data available. Use poll() (or kqueue, which is specific to FreeBSD and MacOS) to monitor is. Usually this is done in your application's main event loop.

If you did not mean for your socket to be nonblocking, then you should set it to blocking more with fcntl():

flags = fcntl(i, F_GETFL, 0); /* add error checking here, please */
flags &= ~O_NONBLOCK;
fcntl(i, F_SETFL, flags); /* add more error checking here! */

But you should be aware that the default blocking state of sockets (and all file descriptors) is blocking, so if your socket is in nonblocking mode then that means someone or something has manually made it nonblocking.

In blocking mode, the recv call will block and wait for more data instead of returning EAGAIN (or EWOULDBLOCK which is the same thing as EAGAIN).

Tags:

C

Sockets

Macos