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.
非常感谢你的帮助。
2 个解决方案
#1
8
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.
将套接字设置为非阻塞(O_NONBLOCK)。套接字仍在忙于发送先前的消息。您不能发送另一个直到第一个完成发送。这就是为什么睡了。
Don't set it to non-blocking, or try again after select
says you can.
不要将其设置为非阻塞,或者在select语句之后再尝试。
#2
0
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))而不阻塞,则认为文件描述符已准备就绪。
#1
8
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.
将套接字设置为非阻塞(O_NONBLOCK)。套接字仍在忙于发送先前的消息。您不能发送另一个直到第一个完成发送。这就是为什么睡了。
Don't set it to non-blocking, or try again after select
says you can.
不要将其设置为非阻塞,或者在select语句之后再尝试。
#2
0
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))而不阻塞,则认为文件描述符已准备就绪。