Polling non-blocking sockets

It is possible to configure sockets to use a non-blocking operation. One way to do this is by calling fcntl() with the O_NONBLOCK flag (ioctlsocket() with the FIONBIO flag on Windows), although other ways also exist. Once in non-blocking mode, a call to recv() with no data will return immediately. See Chapter 13Socket Programming Tips and Pitfalls, for more information.

A program structured with this in mind could simply check each of its active sockets in turn, continuously. It would handle any socket that returned data and ignore any socket that didn't. This is called polling. Polling can be a waste of computer resources since most of the time, there will be no data to read. It also complicates the program somewhat, as the programmer is required to manually track which sockets are active and which state, they are in. Return values from recv() must also be handled differently than with blocking sockets.

For these reasons, we won't use polling in this book.