sendto : Resource temporarily unavailable (errno 11)

孤者浪人 提交于 2019-12-19 03:23:04

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/5737493/sendto-resource-temporarily-unavailable-errno-11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!