C语言 错误:11,资源暂时不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13554691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:34:43  来源:igfitidea点击:

Errno: 11, Resource Temporarily Unavailable

csocketsudp

提问by rharrison33

I am using c sockets to implement a reliable UDP protocol. I am using the following code to set a timeout on a socket in which I'm waiting for an acknowledgement. I am not sure why I am getting errno 11, resource temporarily unavailable.

我正在使用 c 套接字来实现可靠的 UDP 协议。我正在使用以下代码在等待确认的套接字上设置超时。我不确定为什么我会收到 errno 11,资源暂时不可用。

        //set timer for recv_socket
        struct timeval tv;
        tv.tv_usec = TIMEOUT_MS;

        if(setsockopt(rcv_sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0){
            printf("Error setting the socket timeout.\n");
        }

        int recv_msg_len;
        if(recv_msg_len = recvfrom(rcv_sock, ackBuffer,sizeof(ackBuffer), 0,
               (struct sockaddr *) &servAddr2, &fromSize) < 0){
            //timeout reached
            printf("Error Reporting: %d : %s\n", errno, strerror(errno));
            num_timeouts++;
        }

I have also tried the select method that was mentioned in the comments. I have the following code inside a loop, but the recvfrom never times out.

我也尝试过评论中提到的 select 方法。我在循环中有以下代码,但 recvfrom 永远不会超时。

        fd_set set;
        FD_ZERO(&set);      /* empties the set */
        FD_CLR(rcv_sock,&set);    /* removes FD from the set */
        FD_SET(rcv_sock,&set);    /* adds FD to the set */

        if(select(rcv_sock + 1, &set, NULL, NULL, &tv) < 0){
            printf("\nError Reporting: %d : %s\n\n", errno, strerror(errno));
            return -1;
        }


        if(!FD_ISSET(rcv_sock,&set)){   /* true if FD is in the set */
            printf("socket is not set properly.\n");
        }

回答by alk

When calling recvfrom()on a blocking socket and a time out had been set using setsockopt()it is normal to get the error EAGAIN (11)in case the call to recvfrom()timed out (that is: no data was received in the time period specified as time out).

当调用recvfrom()阻塞套接字并使用超时设置时,如果调用超时(即:在指定为超时的时间段内没有接收到数据),setsockopt()通常会出现错误。EAGAIN (11)recvfrom()

Verbatim from man recvfrom:

逐字来自man recvfrom

RETURN VALUE

...

ERRORS

... .

EAGAIN or EWOULDBLOCK 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 was received. ...

返回值

...

错误

……

EAGAIN 或 EWOULDBLOCK 套接字被标记为非阻塞并且接收操作将阻塞,或者已经设置了接收超时并且超时在接收数据之前到期。...

To get around this: Just call recvfrom ()again ... ;-)

要解决此问题:只需recvfrom ()再次致电...;-)

回答by user3704779

For me, the problem was due to ipV6 packets arriving on a UDP socket bound to a particular port. These were triggering the select() but when I tried to read them using recvfrom() the call returned "Resource temporarily unavailable". I don't need IPV6 for my application so I simply disabled it via sysctl.conf. Problem now gone away!

对我来说,问题是由于 ipV6 数据包到达绑定到特定端口的 UDP 套接字。这些正在触发 select() 但是当我尝试使用 recvfrom() 读取它们时,调用返回“资源暂时不可用”。我的应用程序不需要 IPV6,所以我只是通过 sysctl.conf 禁用它。现在问题消失了!