Using select()

This lesson presents an example to explain the usage of select().

We'll cover the following...

To make this more concrete, let’s examine how to use select() to see which network descriptors have incoming messages upon them. The code excerpt below shows a simple example.

Press + to interact
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
// open and set up a bunch of sockets (not shown)
// main loop
while (1) {
// initialize the fd_set to all zero fd_set readFDs;
fd_set readFDs;
FD_ZERO(&readFDs);
// now set the bits for the descriptors
// this server is interested in
// (for simplicity, all of them from min to max)
int fd;
for (fd = minFD; fd < maxFD; fd++)
FD_SET(fd, &readFDs);
// do the select
int rc = select(maxFD+1, &readFDs, NULL, NULL, NULL);
// check which actually have data using FD_ISSET()
int fd;
for (fd = minFD; fd < maxFD; fd++)
if (FD_ISSET(fd, &readFDs))
processFD(fd);
}
}

This code is actually fairly simple to understand. After some initialization, the server enters an infinite loop. Inside the loop, it uses the FD_ZERO() macro to first clear the set of file descriptors and then uses FD_SET() to include all of the file descriptors from minFD to maxFD in the set. This set of descriptors might represent, for example, all of the network sockets to which the server is paying attention. ...