468,513 Members | 1,837 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

select call is timing out? im stumped

ok ill start off with a little background. I work for a company which makes heavy duty wireless routers/bridges, which work over long distances. Up to 30miles. Now each of our units comes with a scan utility, which can be used to default and change the ip of units. A quick example, say your on a network of 192.168.100.0. You obviously wont be able to comminicate with our unit on your network if the units ip is 206.71.186.46. The scan utility uses broadcasts to let recieve info about the unit and let you change a few things so that you will be able to connect to it. I hope thats clear enough.

Now, im working on a linux version of the client side scan utility. Now what this does is bind a udp socket to each network card installed on the computer. It then procedes to send broadcast probes out each socket.

My problem is in the thread which waits to recieve a probe response from our units when they recieve a broadcast. Im using select() to wait for a response on one of those sockets and then respond. Ive used a packet capture program to capture the packets, so I know that the probe goes out, and the unit sends the appropriate response back to the computer. So the select call should show that their is information waiting on whichever socket it came in on. However it times out instead. About 3/4 of the time this works fine. Heres the code for the problem area.
Expand|Select|Wrap|Line Numbers
  1.         FD_ZERO(&readSet);                      
  2.         NumOfSockets = maxSocket = 0;
  3.         for(int i=0; i<6; i++)            // get all valid sockets from socket array
  4.         {                                       // and add them to select call
  5.             if(socketDescriptor[i] != 0x0)
  6.             {
  7.                 FD_SET(socketDescriptor[i], &readSet);
  8.                 NumOfSockets++;
  9.                 if(maxSocket<socketDescriptor[i])
  10.                 {
  11.                     maxSocket = socketDescriptor[i];
  12.                 }
  13.             }
  14.         }
  15.         time.tv_sec = 1;
  16.         time.tv_usec = 0;
  17.         // wait up to 1 second for a response from the unit
  18.         active = select(maxSocket+1, &readSet, NULL, NULL, &time);
  19.         if (active > 0) 
  20.         {
  21.             UnitDetected=1;
  22.             for(int i=0; i<6; i++)
  23.             {
  24.                 if(socketDescriptor[i]==0)//added to handle a bug which occured if the first nic didnt have a unit attached
  25.                 {
  26.                     continue;
  27.                 }
  28.                 else if (FD_ISSET(socketDescriptor[i], &readSet))
  29.                 {
  30.                     UnitDetected=1;        // if a unit responds, turn this flag on
  31.                       if (recv(socketDescriptor[i], buf, MAX_LINE, 0) < 0) 
  32.                 {    
  33.                         perror("Recv info: ");
  34.                         continue;
  35.                 }
  36.                                   test = (A_UINT16 *)buf;
  37.                                   if(ntohs(*test)==PROBE_RESPONSE_MSG)
  38.                                   {
  39.                                              // does stuff, when probe is recieved.
  40.                                   }
  41.                                   else if(ntohs(*test)==COMMAND_RESPONSE_MSG)
  42.                                   {
  43.                                   // does stuff when a command response is recieved
  44.                                   }
  45.                                   else
  46.                                   {
  47.                         cout << "Invalid unit response" << endl;
  48.                         ProbeRDY = 0;    //set flag off
  49.                         CmdRDY = 0;        //set flag off
  50.                     }
  51.             }
  52.         }
  53.         else if (active == 0)
  54.         {
  55.             cerr << "Select call timed out . . ." << endl;
  56.             ProbeRDY = 0;
  57.             CmdRDY = 0;
  58.         }
  59.         else
  60.         {
  61.             perror("select: ");
  62.                 UnitDetected = 0;
  63.             ProbeRDY = 0;
  64.             CmdRDY = 0;
  65.         }
  66.     }


Sorry for the length! I tried to shorten it as much as possible. Any idea's or possible work arounds would be helpfull, thanks!
Aug 24 '06 #1
2 3582
i added some code to double check that each socket is actually timing out, in the part that handles timeouts in the above code. So far that seems like its solved the problem, but i havent had time to really test it yet. It seems to me like the select call has a few problems. I might just write my own select method to simplify things if it is a problem with select()
Aug 25 '06 #2
Hi Ashaman0,

does select parameter 'readSet' in your sample contains the data it receives from the socket? Am a bit confuse about this function. I really need help.

Thanks
Jan 19 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Terencetrent | last post: by
7 posts views Thread by Brian | last post: by
7 posts views Thread by C.Joseph Drayton | last post: by
4 posts views Thread by Ian Richardson | last post: by
18 posts views Thread by Ron Garret | last post: by
2 posts views Thread by Richard Cranium | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.