错误:11,资源暂时不可用 [英] Errno: 11, Resource Temporarily Unavailable
问题描述
我正在使用 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.\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++;
}
我也试过评论中提到的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("\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");
}
推荐答案
当在阻塞套接字上调用 recvfrom()
并且使用 setsockopt()
设置超时时> 如果调用 recvfrom()
超时(即:在指定的时间段内没有收到数据),出现错误 EAGAIN (11)
是正常的超时).
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 ... ;-)
这篇关于错误:11,资源暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!