Errno:11,资源暂时不可用 [英] Errno: 11, Resource Temporarily Unavailable

查看:39
本文介绍了Errno:11,资源暂时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

        //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.
");
        }

        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
", errno, strerror(errno));
            num_timeouts++;
        }

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

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.

        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("
Error Reporting: %d : %s

", errno, strerror(errno));
            return -1;
        }


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

推荐答案

当在阻塞套接字上调用 recvfrom() 并且使用 setsockopt() 如果对 recvfrom() 的调用超时(即:在指定的时间段内没有收到数据超时).

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

逐字逐句来自man recvfrom:

返回值

...

错误

....

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

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

要解决这个问题:只需再次调用 recvfrom () ... ;-)

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

这篇关于Errno:11,资源暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆