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
- FD_ZERO(&readSet);
- NumOfSockets = maxSocket = 0;
- for(int i=0; i<6; i++) // get all valid sockets from socket array
- { // and add them to select call
- if(socketDescriptor[i] != 0x0)
- {
- FD_SET(socketDescriptor[i], &readSet);
- NumOfSockets++;
- if(maxSocket<socketDescriptor[i])
- {
- maxSocket = socketDescriptor[i];
- }
- }
- }
- time.tv_sec = 1;
- time.tv_usec = 0;
- // wait up to 1 second for a response from the unit
- active = select(maxSocket+1, &readSet, NULL, NULL, &time);
- if (active > 0)
- {
- UnitDetected=1;
- for(int i=0; i<6; i++)
- {
- if(socketDescriptor[i]==0)//added to handle a bug which occured if the first nic didnt have a unit attached
- {
- continue;
- }
- else if (FD_ISSET(socketDescriptor[i], &readSet))
- {
- UnitDetected=1; // if a unit responds, turn this flag on
- if (recv(socketDescriptor[i], buf, MAX_LINE, 0) < 0)
- {
- perror("Recv info: ");
- continue;
- }
- test = (A_UINT16 *)buf;
- if(ntohs(*test)==PROBE_RESPONSE_MSG)
- {
- // does stuff, when probe is recieved.
- }
- else if(ntohs(*test)==COMMAND_RESPONSE_MSG)
- {
- // does stuff when a command response is recieved
- }
- else
- {
- cout << "Invalid unit response" << endl;
- ProbeRDY = 0; //set flag off
- CmdRDY = 0; //set flag off
- }
- }
- }
- else if (active == 0)
- {
- cerr << "Select call timed out . . ." << endl;
- ProbeRDY = 0;
- CmdRDY = 0;
- }
- else
- {
- perror("select: ");
- UnitDetected = 0;
- ProbeRDY = 0;
- CmdRDY = 0;
- }
- }
Sorry for the length! I tried to shorten it as much as possible. Any idea's or possible work arounds would be helpfull, thanks!