Errno 35 (EAGAIN) is returned when calling recv

I have a socket that waits for a recv and then, after receiving data, sends the data forward for processing. However, then it comes back again for recv and this time it gets nothing, returns -1, and when printing errno, it prints 35 (this EAGAIN

).

This only happens on MAC OS Lion, for other OS it works fine

do{
 rc = recv(i, buffer, sizeof(buffer), 0);
 if (rc < 0){
      printf("err code %d", errno); 
 }
 if(rc == 0){ 
      //Code for processing the data in buffer 
      break; 
 } 
      ....
}while(1);

      

EDIT: Fixed indentation and errno

+3


source to share


2 answers


You either set the socket to non-blocking mode or enable the receive timeout. Here from recv(2)

on 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, sorry for re-quoting. 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)

it to figure out the actual problem.

+4


source


Your socket is in non-blocking mode. EAGAIN

is a normal return from recv()

(and other system calls) when there is no data available to read. In this sense, it is not a mistake.

If you want your socket to be non-blocking, you need to monitor it to know when it has data available and only call it recv()

when there is data. Use poll()

(or kqueue, which is FreeBSD and MacOS specific) for monitoring. This is usually done in the main event loop of the application.

If you didn't mean that your socket was not blocked, you should set it to block 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 for sockets (and all file descriptors) is blocking, so if your socket is in non-blocking mode then someone or something has manually made it non-blocking.

In blocking mode, the call recv

blocks and waits for more data instead of returning EAGAIN

(or EWOULDBLOCK

, which is the same as EAGAIN

).

+1


source







All Articles