本文介绍了实施下与选择UDP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是我使用选择()
从客户端等待数据实施C.一个套接字编程项目。我有两个UDP套接字和选择()
总是忽略我的插槽中。
大家能否简要介绍一下我应该从哪里开始找呢?这就是我的服务器是做
waitThreshold.tv_sec = 5000;
waitThreshold.tv_usec = 50;
如果(SD> SD1)
max_sd =(SD + 1);
否则,如果(SD1> SD)
max_sd =(SD1 + 1);
FD_ZERO(安培; read_sds);
FD_SET(SD,&安培; read_sds);
FD_SET(SD1,&安培; read_sds);RET =选择(max_sd,&安培; read_sds,NULL,NULL,&安培; waitThreshold);
如果(保留℃,){
的printf(\\ n选择抛出的异常\\ n);
返回0;
}否则如果(FD_ISSET(SD,&安培; read_sds)){
// code代表孔一
}否则如果(FD_ISSET(SD1,&安培; read_sds)){
// code的插座二个
}
解决方案
您必须设置和重置后,每次迭代
如果(SD> SD1)
max_sd =(SD + 1);
否则,如果(SD1> SD)
max_sd =(SD1 + 1);
//一些code
而(1){
waitThreshold.tv_sec = 5000;
waitThreshold.tv_usec = 50;
FD_ZERO(安培; read_sds);
FD_SET(SD,&安培; read_sds);
FD_SET(SD1,&安培; read_sds);
RET =选择(max_sd,&安培; read_sds,NULL,NULL,&安培; waitThreshold);
如果(RET℃下)
{
的printf(\\ n选择抛出的异常\\ n);
返回0;
}
否则,如果(FD_ISSET(SD,&安培; read_sds))
{// code代表孔一}
否则,如果(FD_ISSET(SD1,&安培; read_sds))
{// code的插座二个}< / pre>< / code>
}
现在它会解决你的问题。
I am implementing a socket programming project in C. I am using select()
for waiting for data from client. I have two UDP sockets and select()
is always ignoring one of my sockets.
Can anybody briefly describe where should I start looking for it? This is what my server is doing
waitThreshold.tv_sec = 5000;
waitThreshold.tv_usec = 50;
if (sd > sd1)
max_sd = (sd + 1);
else if(sd1 > sd)
max_sd = (sd1 + 1);
FD_ZERO(&read_sds);
FD_SET(sd, &read_sds);
FD_SET(sd1, &read_sds);
ret = select(max_sd, &read_sds, NULL, NULL, &waitThreshold);
if (ret < 0) {
printf("\nSelect thrown an exception\n");
return 0;
} else if (FD_ISSET(sd, &read_sds)) {
// code for socket one
} else if (FD_ISSET(sd1, &read_sds)) {
// code for socket two
}
解决方案
You must set and reset after each iteration
if(sd > sd1)
max_sd = (sd + 1);
else if(sd1 > sd)
max_sd = (sd1 + 1);
//some code
while(1){
waitThreshold.tv_sec = 5000;
waitThreshold.tv_usec = 50;
FD_ZERO(&read_sds);
FD_SET(sd, &read_sds);
FD_SET(sd1, &read_sds);
ret = select(max_sd, &read_sds, NULL, NULL, &waitThreshold);
if(ret <0)
{
printf("\nSelect thrown an exception\n");
return 0;
}
else if(FD_ISSET(sd, &read_sds))
{ // code for socket one }
else if(FD_ISSET(sd1, &read_sds))
{ // code for socket two }</pre></code>
}
Now it will solve your problem.
这篇关于实施下与选择UDP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!