Examples: Use 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 contain links to usage notes on specific APIs. If you need more details on 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() function returns a socket descriptor representing 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() function gets a unique name for the socket.
  3. The listen() allows the server to accept incoming client connections.
  4. The spawn() function creates each of the worker jobs.
  5. In this example, the first close() function closes the listening socket.

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

The second example uses the following sequence of function 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() function waits for an incoming client connection.
  2. The recv() function receives a message from the client.
  3. The send() function echoes data back to the client.
  4. The close() function ends the worker job.
Related reference
Example: Generic client
Related information
spawn()
bind()
socket()
listen()
close()
accept()
send()
recv()