1

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 
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Shreyas Kale
  • 65
  • 1
  • 3
  • 8
  • A hole on the sock... Oh wait, no, it's a socket. –  Mar 23 '13 at 21:13
  • 1
    There's not really enough information here for anyone to know what you're doing wrong. See if you can make a minimal example and post the code. Are you adding both sockets to the relevant fd_sets? Is the first parameter of select() set to the highest-valued file descriptor _plus one_? – svk Mar 23 '13 at 21:13
  • Yes it is set to highest plus one. I will edit and post some of mycode here, I couldn't figure out how much and what to post coz its a big project – Shreyas Kale Mar 23 '13 at 21:18
  • I have added code in my question, any comments?? @svk http://stackoverflow.com/users/2151753/svk – Shreyas Kale Mar 23 '13 at 21:24
  • As far as I can see that code looks fine. You don't need the else clauses -- you might have data on both sd and sd1 -- but I don't think that should be a problem unless you're getting totally flooded with data on the first socket. The problem might not be in this part of your code. See if you can make a small example that shows your problem and that is complete, so it can be compiled and run. – svk Mar 23 '13 at 21:35
  • @svk I found solution to my problem. I kept those if elses in while(1) loop. In every iteration, I have to FD-ZERO and FD_SET those readfds. I did that outside while so, everytime it was taking only one socket as its corresponding bit was always set. Thanks for your response. – Shreyas Kale Mar 23 '13 at 21:45
  • Instead of the `else/if`'s, you should loop over the `read_sds` set. At present you are calling select() once for every read action that you take, which is wasteful: you should call it once and process all the read actions. At least get rid of the `else`s. – user207421 Mar 24 '13 at 04:05
  • possible duplicate of [UDP sockets use of select()](http://stackoverflow.com/questions/15480599/udp-sockets-use-of-select) – tripleee Nov 29 '14 at 10:09

1 Answers1

2

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.

suraj_fale
  • 978
  • 2
  • 21
  • 53