C语言 sendto : 资源暂时不可用 (errno 11)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5737493/
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
sendto : Resource temporarily unavailable (errno 11)
提问by Jary
I am having a problem with sendto.
我在使用 sendto 时遇到问题。
I have a receiver who receives UPD packets with recvfrom and then replies to the sender using sendto.
我有一个接收者使用 recvfrom 接收 UPD 数据包,然后使用 sendto 回复发送者。
Unfortunately, I am getting errno 11 (Resource temporarily unavailable). I am using two sockets.
不幸的是,我收到 errno 11(资源暂时不可用)。我正在使用两个插座。
The first packet is actually sent but not the ones afterwards:
第一个数据包实际上被发送,但不是之后的:
sendto :: Success
发送到 :: 成功
error: 0.
错误:0。
sendto :: Resource temporarily unavailable
sendto :: 资源暂时不可用
error: 11.
错误:11。
sendto :: Resource temporarily unavailable
sendto :: 资源暂时不可用
...
...
This is an extract of my code:
这是我的代码的摘录:
int sockfd, sockSend;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
perror("socket");
if ((sockSend = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
perror("socket");
if (fcntl(sockfd, F_SETOWN, getpid()) < 0) {
perror("fcntl");
}
if (fcntl(sockfd, F_SETFL, O_RDONLY | O_NONBLOCK | FASYNC) < 0) {
perror("fcntl");
}
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
< 0)
perror("bind");
And in a SIGIO handler:
在 SIGIO 处理程序中:
len = sizeof(recv_addr);
char buffer[payload];
bzero(buffer, payload);
n = recvfrom(sockfd, buffer, payload, MSG_DONTWAIT, (struct sockaddr *)&recv_addr, &len);
while (n > 0) {
sprintf(response, "%d\n%d\n%d\n", items, target_buf, pb_sp);
sendto(sockSend, response, strlen(response), 0, (struct sockaddr *) &recv_addr, sizeof(recv_addr));
// sleep(1);
perror("sendto :");
printf("error: %d.\n", errno);
}
Could this issue come because the port is still hot, and I need to wait before reusing it? I've tried to change port but it hasn't helped.
这个问题是不是因为端口仍然很热,我需要等待才能重新使用它?我尝试更改端口,但没有帮助。
Update: If the sleep(1) is commented out, then the packets actually get send!
更新:如果 sleep(1) 被注释掉,那么数据包实际上会被发送!
Thanks a lot for your help.
非常感谢你的帮助。
回答by ikegami
The error you are getting:
你得到的错误:
EAGAIN or EWOULDBLOCK: The socket is marked nonblocking and the requested operation would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
EAGAIN 或 EWOULDBLOCK:套接字被标记为非阻塞并且请求的操作将阻塞。POSIX.1-2001 允许在这种情况下返回任一错误,并且不要求这些常量具有相同的值,因此可移植应用程序应检查这两种可能性。
You set the socket to non-blocking (O_NONBLOCK). The socket is still busy sending the previous message. You cannot send another until the first has finished sending. That's why sleeping helped.
您将套接字设置为非阻塞 (O_NONBLOCK)。套接字仍然忙于发送前一条消息。在第一个完成发送之前,您不能发送另一个。这就是为什么睡觉有帮助。
Don't set it to non-blocking, or try again after selectsays you can.
不要将其设置为非阻塞,或在select说可以后重试。
回答by Alifornia
If you have to set the socket to non-blocking, you can do it safely (and only?) using select:
如果您必须将套接字设置为非阻塞,您可以安全地(并且仅?)使用select:
select()and pselect()allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking.
select()和pselect()允许程序监视多个文件描述符,等待一个或多个文件描述符“准备好”进行某类 I/O 操作(例如,可能的输入)。如果可以在不阻塞的情况下执行相应的 I/O 操作(例如 read(2)),则认为文件描述符已准备就绪。

