Multiclient servers

Servers not only need to be continuously waiting for potential client connections, but they also need to be able to handle simultaneous requests from multiple clients. It is generally not feasible to put clients in a queue and process their requests one after the other.

The problem

Blocking instructions, from a TCP server, (meaning those that cause the temporary stop of the server's execution until a specified event occurs) are:

Reading or writing on a client's channel can therefore put other reading or writing operations on hold for another client as well as the acceptance of new clients. To solve this problem, several solutions are available.

Multi-tasking solutions

Multi-tasking solutions consist of deploying a task per client or even two tasks if the client can both read and write on the channel at a given time (For example, in the case of a chat program that would receive characters typed by a client while transmitting to this client characters received from other clients in the same conversation group). To deploy multi-tasking, there are two solutions (that can possibly be combined):

The single task solution

The other solution is to not execute accept(), read() or write() without first checking that the event they are waiting for (which can therefore make them blocking) has not already occurred. The blocking instruction select() will allow you to simultaneously monitor the 3 types of events for a set of sockets.