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.