Reputation: 65
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
}
Upvotes: 1
Views: 19418
Reputation: 960
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.
Upvotes: 2