SENDTO:资源暂时不可用(错误11) [英] sendto : Resource temporarily unavailable (errno 11)

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

问题描述

我有一个的sendto问题。

我有谁与recvfrom的UPD接收数据包,然后接收器使用的sendto回复给发件人。

不幸的是,我得到错误号11(资源暂时不可用)。我用的两个插槽。

第一个数据包实际发送,但不是那些算账:

SENDTO ::成功

错误:0

SENDTO ::资源暂时不可用

错误:11

SENDTO ::资源暂时不可用

...

这是我的code的摘录:

  INT的sockfd,sockSend;    如果((的sockfd =插座(AF_INET,SOCK_DGRAM,0))小于0)
            PERROR(插座);    如果((sockSend =插座(AF_INET,SOCK_DGRAM,0))小于0)
            PERROR(插座);    如果(的fcntl(的sockfd,F_SETOWN,GETPID())小于0){
            PERROR(的fcntl);
    }
    如果(的fcntl(的sockfd,F_SETFL,O_RDONLY | O_NONBLOCK | FASYNC)小于0){
            PERROR(的fcntl);
    }    如果(绑定(的sockfd,(结构sockaddr *)及serv_addr,sizeof的(serv_addr))
                    < 0)
            PERROR(绑定);

而在一个SIGIO处理程序:

  LEN = sizeof的(recv_addr);
    字符缓冲区[负载]
    bzero(缓冲,有效载荷);
    N = recvfrom的(的sockfd,缓冲,有效载荷,MSG_DONTWAIT,(结构sockaddr *)及recv_addr,和放大器; LEN);    而(N 0){                            sprintf的(回应,%d个\\ n%d个\\ n%d个\\ N,物品,target_buf,pb_sp);
                            SENDTO(sockSend,响应,strlen的(响应),0,(结构sockaddr *)及recv_addr,sizeof的(recv_addr));
                            //睡眠(1);                            PERROR(SENDTO:);
                            的printf(错误:%d个\\ n,错误号);     }

难道这个问题来,因为港口仍然是热的,我需要重新使用之前要等待?我试图更改端口,但它并没有帮助。

更新:如果睡眠(1)已被注释掉,则数据包实际获得发送

非常感谢你的帮助。


解决方案

你所得到的错误:

EAGAIN或EWOULDBLOCK:该插槽上都标非阻塞和请求的操作将阻塞。 POSIX.1-2001允许这种情况下要返回任一错误,并且不需要这些常数为具有相同的值,因此,一个便携式应用应该检查这两种可能性。

您设置套接字非阻塞(O_NONBLOCK)。插座仍在忙于发送previous消息。你不能再派直到第一个完成发送。这就是为什么睡眠有帮助。

不要将它设置为非阻塞,或在再试选择说就可以了。

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.

解决方案

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.

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

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