You need to do something like this:
This is a very simple answer and requires definitions and prototypes, however this should give you an example of a basic choice.
this code works both on freebsd, ubuntu, and on my windows computer (given that you have the correct headers). It has also been reduced and some definitions removed, sucking as a socket descriptor, because they are pretty much what they are.
struct timeval timeout;
int rc
fd_set wfdset,rfdset,errfdset;
//Do some checks put them in either read fdset or write fdset or error fdset
FD_SET (socket_sd, &rfdset);
timeout.tv_sec = 0;
timeout.tv_usec = 250 * 1000;
rc = select (maxfds + 1, &rfdset, &wfdset, NULL, &timeout);
//loop through the sockets and read from them at this point.
The choice is ported to Win32 as well as to UNIX, although this is not recommended if you are doing heavy socket work on unix IE: FreeBSD. use kqueue or epoll or the like if you need to manage your sockets deeper and more efficiently.
source
share