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.
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:
- 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.
- After the socket descriptor is created, the bind() function
gets a unique name for the socket.
- The listen() allows the server to accept incoming client
connections.
- The spawn() function creates each of the worker jobs.
- 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:
- 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.
- The recv() function receives a message from the client.
- The send() function echoes data back to the client.
- The close() function ends the worker job.