简体   繁体   English

错误 35 (EAGAIN) 在 recv 调用中返回

[英]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 ).但是,然后它再次进行 recv,这次它没有收到任何返回 -1 并且在打印 errno 时它打印 35 (这是EAGAIN )。

This happens only on MAC OS Lion operating system, for other OS this runs perfectly fine这只发生在 MAC OS Lion 操作系统上,对于其他操作系统,它运行得非常好

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:这是来自 mac 上的recv(2)

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. [EAGAIN]套接字被标记为非阻塞,接收操作会阻塞,或者已经设置了接收超时,并且在接收数据之前超时到期。

Edit 0:编辑0:

Hmm, apologies for quoting again.嗯,抱歉再次引用。 This time from intro(2) :这次来自intro(2)

11 EDEADLK Resource deadlock avoided. 11 EDEADLK避免了资源死锁。 An attempt was made to lock a system resource that would have resulted in a deadlock situation.试图锁定可能导致死锁情况的系统资源。

... ...

35 EAGAIN Resource temporarily unavailable. 35 EAGAIN资源暂时不可用。 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.只需使用strerror(3)找出实际问题。

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. EAGAIN是当没有数据可供读取时从recv() (和其他系统调用)的正常返回。 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.如果你的意思是你的套接字非阻塞,那么你需要对其进行监控,找出当有可用的数据,只有调用recv()当有可用的数据。 Use poll() (or kqueue, which is specific to FreeBSD and MacOS) to monitor is.使用poll() (或 kqueue,特定于 FreeBSD 和 MacOS)来监控。 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() :如果你的意思不是让你的套接字是非阻塞的,那么你应该使用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 ).在阻塞模式下, recv调用将阻塞并等待更多数据,而不是返回EAGAIN (或与EAGAIN相同的EWOULDBLOCK )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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