sendto:资源暂时不可用(errno 11)

发布于 2024-11-02 17:55:39 字数 1682 浏览 1 评论 0原文

我在使用 sendto 时遇到问题。

我有一个接收者,它使用recvfrom接收UPD数据包,然后使用sendto回复发送者。

不幸的是,我收到 errno 11(资源暂时不可用)。我正在使用两个套接字。

第一个数据包实际上已发送,但后面的数据包并未发送:

sendto :: Success

error: 0.

sendto :: Resourcetemporary unavailable

error: 11.

sendto :: Resourcetemporary unavailable

...

的摘录:

    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");

这是我的代码 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);

     }

这个问题是否会因为端口仍然很热而出现,并且我需要等待才能重新使用它?我尝试过更改端口,但没有帮助。

更新:如果 sleep(1) 被注释掉,那么数据包实际上会被发送!

非常感谢您的帮助。

I am having a problem with sendto.

I have a receiver who receives UPD packets with recvfrom and then replies to the sender using sendto.

Unfortunately, I am getting errno 11 (Resource temporarily unavailable). I am using two sockets.

The first packet is actually sent but not the ones afterwards:

sendto :: Success

error: 0.

sendto :: Resource temporarily unavailable

error: 11.

sendto :: Resource temporarily unavailable

...

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:

    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!

Thanks a lot for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-11-09 17:55:39

您收到的错误:

EAGAIN 或 EWOULDBLOCK:套接字被标记为非阻塞,并且请求的操作将阻塞。 POSIX.1-2001 允许在这种情况下返回任一错误,并且不要求这些常量具有相同的值,因此可移植应用程序应检查这两种可能性。

您将套接字设置为非阻塞 (O_NONBLOCK)。套接字仍然忙于发送上一条消息。在第一个发送完成之前,您无法发送另一个。这就是为什么睡眠有帮助。

不要将其设置为非阻塞,或者在 select 表示可以后重试。

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.

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.

Don't set it to non-blocking, or try again after select says you can.

滥情空心 2024-11-09 17:55:39

如果您必须将套接字设置为非阻塞,您可以使用 select 安全地(并且只能?)完成此操作:

select()pselect() 允许程序监视多个文件描述符,等待一个或多个文件描述符为某一类文件“准备好”。 I/O 操作(例如,可能的输入)。如果可以在不阻塞的情况下执行相应的 I/O 操作(例如 read(2)),则文件描述符被视为就绪。

If you have to set the socket to non-blocking, you can do it safely (and only?) using 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文