Examples: Using multiple accept() APIs to handle incoming requests

These examples show how to design a server program that uses the multiple accept() model for handling incoming connection requests.

When the multiple accept() server starts up, it does a socket(), bind(), and listen() as normal. It then creates a pool of worker jobs and gives each worker job the listening socket. Each multiple accept() worker then calls accept().

The following figure illustrates how the server, worker, and client jobs interact when the system uses the multiple accept() server design.

Server, worker, and client job interaction when you use the multiple accept() server design

Flow of socket events: Server that creates a pool of multiple accept() worker jobs

The following sequence of the socket calls provides a description of the figure. It also describes the relationship between the server and worker examples. Each set of flows contains links to usage notes on specific APIs. If you need more details about the use of a particular API, you can use these links. The first example uses the following socket calls to create a child process:

  1. The socket() API returns a socket descriptor, which represents an endpoint. The statement also identifies that the INET (Internet Protocol) address family with the TCP transport (SOCK_STREAM) is used for this socket.
  2. After the socket descriptor is created, the bind() API gets a unique name for the socket.
  3. The listen() API allows the server to accept incoming client connections.
  4. The spawn() API creates each of the worker jobs.
  5. In this example, the first close() API closes the listening socket.

Socket flow of events: Worker job that multiple accept()

The second example uses the following sequence of API calls:

  1. After the server has spawned the worker jobs, the listen socket descriptor is passed to this worker job as a command line parameter. The accept() API waits for an incoming client connection.
  2. The recv() API receives a message from the client.
  3. The send() API echoes data back to the client.
  4. The close() API ends the worker job.