#include #include #include // This is an example of how 'select' system call works on UNIX. // The syntax for 'select' on WinSock is the same. The include // header file has to be changed to // #include // #inlucde // The syntax for FD_SET and FD_ISSET is slightly different // on WinSock. See attached example. // // Assume a number of sockets have been opened before, and socket IDs // are stored in sockId void Polling(int sockId[], int numSocks) { fd_set fdvar; int numReady = 0; struct timeval timer; int i; timer.tv_sec = 3; timer.tv_usec = 500; // the time-out is set to be 3.5 sec. FD_ZERO(&fdvar); // initialize fdvar for (i = 0; i < numSocks; i ++) FD_SET(sockId[i],&fdvar); // copy values of sockid to fdvar // we allow at maximum 16 sockets open simultaneously // numReady indicates how many sockets are ready numReady = select(16,&fdvar,(fd_set *)0,(fd_set *)0,&timer); if (numReady < 0) perr("Selection error"); if (numReady > 0) // some sockets are ready { for (i = 0; i < numSocks; i ++) // check which one is ready if (FD_ISSET(sockId[i],&fdvar) > 0) doWork(sockId[i]); // process the work on socket sockId[i] } }