[英]Errno: 11, Resource Temporarily Unavailable
I am using c sockets to implement a reliable UDP protocol. 我正在使用c套接字来实现可靠的UDP协议。 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.
我不知道为什么我得到错误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. 我也尝试过评论中提到的select方法。 I have the following code inside a loop, but the recvfrom never times out.
我在循环中有以下代码,但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");
}
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()
times out (that is: no data was received in the time period specified as time out). 在阻塞套接字上调用
recvfrom()
并使用setsockopt()
设置超时时,如果对recvfrom()
的调用超时(即:没有收到任何数据setsockopt()
,则获取错误EAGAIN (11)
是正常的。指定为超时的时间段)。
Verbatim from man recvfrom
: 来自
man recvfrom
逐字记录:
RETURN VALUE
返回值
...
...
ERRORS
错误
... .
......
EAGAIN or EWOULDBLOCK The socket is marked nonblocking 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 ()
...... ;-)
For me, the problem was due to ipV6 packets arriving on a UDP socket bound to a particular port. 对我来说,问题是由于ipV6数据包到达绑定到特定端口的UDP套接字。 These were triggering the select() but when I tried to read them using recvfrom() the call returned "Resource temporarily unavailable".
这些是触发select()但当我尝试使用recvfrom()读取它们时,调用返回“资源暂时不可用”。 I don't need IPV6 for my application so I simply disabled it via sysctl.conf.
我的应用程序不需要IPV6,所以我只是通过sysctl.conf禁用它。 Problem now gone away!
问题现在消失了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.