问题描述
你好我创建一个访问代理服务器的等待与UDP连接从客户数据包,并检查是否所有的人都有效抵达或相同的人都被拒绝了。
Hi I'm creating a proxyserver that wait for packets from client with an UDP connection and checks if all of them are effectively arrived or same of them have been rejected .
在这种情况下,我应该发送确认以客户为每个丢失的数据包(带send_ack()),但刚过已发送的第一个ACK选择循环发送无限的ACK永远不会回的,如果部分在其他部分们选择从聆听客户数据(接收_pkt()函数)
In that case I should send an "ack" to the clients for each lost packet (with send_ack() ) but just after have sent the first ack the "if part" of the select loops sending unlimited acks never going back to the "else part" were the select listen data from client (receive _pkt() function )
fd_set rset, allset;
int maxfd, nready;
struct timeval timeout;
timeout.tv_sec = 4;
timeout.tv_usec = 150000;
maxfd = socketfd;
FD_ZERO(&allset);
FD_SET(socketfd, &allset);
rset = allset;
for( i=0; ;i++){
do {
nready=select( (maxfd +1), &rset, NULL, NULL, &timeout);
} while ((nready<0) & (errno==EINTR));
if( nready<0) {
perror("Error main: select failed: ");
exit(32);
}
if( nready==0){
send_ack(socketfd,head);
}
else{
receive_pkt(socketfd, head);
}
}
希望这是很清楚,谢谢你的建议!
Hope it's enough clear, thanks for the advices!
推荐答案
一个人重置 FD_SET RSET
每次调用选择。在
选
呼叫预计现场描述符的位设置,监测和位set字段描述与通知阅读覆盖。
One has to reset the
fd_set rset
before every call to select
. The select
call expects a bit set of field descriptors to monitor and overwrites with a bit set of field descriptors with notifications to read.
for( i=0; ;i++){
do {
rset = allset;
nready=select( (maxfd +1), &rset, NULL, NULL, &timeout);
} while ((nready<0) & (errno==EINTR));
这篇关于UDP套接字使用select()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!