The select() function has several useful features. Given a set of sockets, it can be used to block until any of the sockets in that set is ready to be read from. It can also be configured to return if a socket is ready to be written to or if a socket has an error. Additionally, we can configure select() to return after a specified time if none of these events take place.
The C function prototype for select() is as follows:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
Before calling select(), we must first add our sockets into an fd_set. If we have three sockets, socket_listen, socket_a, and socket_b, we add them to an fd_set, like this:
fd_set our_sockets;
FD_ZERO(&our_sockets);
FD_SET(socket_listen, &our_sockets);
FD_SET(socket_a, &our_sockets);
FD_SET(socket_b...