简体   繁体   中英

Errno 35 (EAGAIN) returned on recv call

I have a socket which waits for recv and then after receiving data, sends data forward for processing. However, then it again goes for recv, and this time it receives nothing returns -1 and when printed the errno it prints 35 (which is EAGAIN ).

This happens only on MAC OS Lion operating system, for other OS this runs perfectly 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: Corrected indentation and errno

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:

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 Resource deadlock avoided. 避免了资源死锁。 An attempt was made to lock a system resource that would have resulted in a deadlock situation.

...

35 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM