Understanding the Role of the Server
In the last chapter you saw how a client program connected to a server process. Compared to connectionless communication, from the client's point of view, the only new step was the "connect" step. The connection was established by use of the connect(2) function.
You will recall that the client program required five basic steps. In their simplest form, these are shown in Figure 8.1 on the left side of the figure, as steps C1 through C5. Steps S1 through S6 depict the six basic steps that a server program will use. The steps shown flow from top to bottom. Two dotted horizontal lines with arrows in the center of the figure show how these events form a relationship between the client and server programs. Figure 8.1 represents the simplest form that a server can take. This model will be further enhanced as you progress through this chapter.
Figure 8.1. Study and compare these connection steps for client and server connections.
The basic steps of the server can now be summarized as follows:
Create a socket.
Bind a socket address (mandatory).
Listen for a connection.
Accept a connection.
Communicate with the client program.
Close the socket.
Notice that, for the server, the bind(2) step 2 (S2 in Figure 8.1) is not optional. The server cannot be contacted by clients unless the client has an address for connecting. On the other hand, the client bind(2) step C2 is optional. In order for a server to be contacted, then, a server address cannot be completely wild.
Notice also that step 3 (S3 in Figure 8.1) is something new. The server must express its interest in accepting connections to the kernel for its socket. This will be discussed further when the listen(2) function is covered.
The process of accepting a connection in step 4 (S4 in Figure 8.1) is another new socket concept. Once a client has connected to the server, the server must then accept the connection. This will be covered in detail when the accept(2) function is covered later in this chapter.
Briefly, the server differs from the client in the following ways:
The server must bind a server address to the socket.
The server listens for a connection.
The server accepts connections.