The Client-Server Paradigm
The Client-Server Paradigm
5.1 Background
In distributed computing, the client-server paradigm refers to a model for network applications
where processes play one of two different roles: a server process, also called a server, is
dedicated to managing access to some resources such as printers or files or a network service,
while client processes, called clients, access the server to use these resources or network service
to complete a task.
Figure 5.1 illustrates the concept of the client-server model. A server process runs on a network-
connected computer called as the server host, to manage a network service provided by that
host. A client host makes use of a client process to access a particular service.
Fig 5.1 The Client-Server Distributed Computing Paradigm
On the Internet, many services like HTTP, FTP, DNS, gopher, etc are client-server applications
which are often known by the protocol that the application implements.
Figure 5.2 illustrates the execution flow of the server process. Once started, a network server
process runs indefinitely, looping continuously to accept requests for sessions from clients. For
each client, the server conducts a service session.
At a higher level of abstraction, a service may be identified using a logical name registered with
a directory or a registry. The logical name will need to be mapped to the physical location of the
server process. If the mapping is performed at run time, then the service's location will be
dynamic, which is said to be location transparent.
5.2.3 Interprocess Communications and Event Synchronization
In the client-server model, the interaction of the processes follows a request-response pattern (see
Figure 5.3). During a service session, a client makes a request to the server, which replies with a
response. The client may make a subsequent request, followed by a reply from the server. This
pattern may repeat indefinitely until the session is concluded.
Fig 5.3 The request response pattern of IPC in the Client-Server model
For each request issued, a client must wait for the reply from the server before it can proceed
further. For example, one of the simplest network services is the Daytime service, whereby a
client process simply obtains a timestamp (the time of day on the server host) from the server
process. Translated into plain English, the dialog during a service session of this protocol
proceeds as follows:
The dialog in each session follows a pattern prescribed in the protocol specified for the service.
Among other things, the specification defines (1) the sequence of interprocess communications
between the client and the server, (2) the syntax and semantics of each request and response, and
(3) the action expected of each side upon receiving a particular request or response.
Again, using the simple Daytime service as an example, the protocol specifies the following:
• There is no need for any syntax in the request from the client, as its contact with the server
automatically implies a request for time.
• The response is a timestamp as a character string formatted according to the specification of
the protocol.
Presentation layer: On the server side, a user interface (UI) is needed to allow the server
process to be started; typically, a command-line execution is sufficient. On the client side, a user
interface needs to be provided by the client process. Using such an interface, a user on the client
host may request the service and receive the response from the server.
Application logic layer: On the server side, the time of day needs to be obtained from the
system and sent to the client host. On the client side, the request from the user will need to be
forwarded to the server, and the response from the server will need to be displayed to the user.
Service layer: The services required to support the application are (1) on the client side, a
readout of the server host's clock (for the timestamp); and (2) on both sides, an IPC mechanism.
The functionalities of the three layers must be present in the combined software. Some of the
functionalities belong in the client, some in the server. For large-scale applications, it is
advisable to design the software so that the functionalities of the three layers are encapsulated in
separate software modules as shown in figure 5.5.
Client-Side Software
Presentation logic: The presentation layer provides the interface for a user of the client process.
A class called DaytimeClientl.java encapsulates the client-side presentation logic. The code in
this class is concerned solely with obtaining the server address as input from the user and
displaying the output in the form of timestamp to the user.
Service Logic: The MyClientDatagramSocket.java class provides the details of the service, in
this case using the datagram socket API. There are at least two significant advantages to
separating the three layers of logic into different software modules:
• Each module can be developed by people with special skills to focus on a module for which
they have expertise. Software engineers who are skilled in user interface may concentrate on
developing the modules for the presentation logic, while those specializing in application and
service logic may focus on developing other modules.
• The separation allows modifications to be made to the logic at one layer without requiring
changes to be made at the other layers. For example, the user interface can be changed from text
mode to graphical mode without necessitating changes in the application logic or the service
logic. Likewise, changes made in the application logic should be transparent to the presentation
layer.
Server-Side Software
Presentation Logic: Typically, there is very little presentation logic on the server side. In this
case, the only user input is for the server port, which, for simplicity, is handled using a
command-line argument.
Service Logic: The MyServerDatagramSocket class provides the details of the IPC service,
using the datagram socket API which is similar to the MyClientDatagram class, with the
exception that the receive Message method returns an object of the DatagramMessage class
which contains the sender's address in addition to the message itself. The sender's address is
needed by the server in order for the server to send a request to the client. The methods
employed to obtain the sender's address from a datagram received are getAddress and getHost,
whose descriptions are shown in Table 5.1.
Client-Side Software
Presentation logic: Here we use a Java class called DaytimeClient2 which is the same as
DaytimeClient1 except for a change in the name of the "helper" class, DaytimeClientHelper2.
The getTimestamp ( ) method in DaytimeClientHelper2 now uses the stream-mode socket API,
but the details are transparent to DaytimeClient2.
Server-Side Software
Presentation logic: The code for DaytimeServer2 is identical to that of DaytimeServer1. The
only user input is for the server port, which, for simplicity, is handled using a command-line
argument.
Application logic: The code for DaytimeServer2 uses the stream mode socket API to accept a
connection. The Socket reference returned (for the data socket) is then used to instantiate a
MyStreamSocket object, whose sendMessage method is employed to transmit a timestamp to
the client at the other end of the connection.
Service logic: The same wrapper class used for the client, MyStreamSocket, is used in our server
as well, as it contains the necessary methods for stream-mode IPC. Note that a different class or
even mechanism for providing the service logic is possible if the server software were developed
independently of the client software.
To reiterate, in switching to use the stream-mode socket API, the only modules that required
significant modifications are those that provide the service logic on either side.
Because of its inherent complexity, network software is notoriously difficult to test. Even a
simple network service such as Daytime poses a challenge to novices. Following is some advice
that you may find helpful:
• Use the three-layered software architecture and modularize each layer on both the client and
the server sides.
• Use an incremental or stepwise approach in developing each module. Starting with stubs for
each method, compile and test a module each time after you put in additional details.
• Develop the client first. It is sometimes useful to employ an Echo server that is known to be
correct and that uses a compatible IPC mechanism to test the client independently of the server.
Doing so allows you to develop the client separately from the server.
• Use diagnostic messages throughout the source code to report the progress of the program
during run time.
• Test the client-server suite on a single machine before running the programs on separate
machines.
The Daytime client-server examples are useful as an introduction to the development of a
network service. The simplicity of the protocol is such that each service session involves merely
one round of message exchange between the two processes.
EchoServer1.
Fig 5.6 A sequence diagram illustrating two interleaved sessions with EchoServer1
Throughout a session, the server maintains its connection with the client and exchanges data with
the connected client via a data socket dedicated to that client. If another client connects to the
server while it is already occupied with a session, the client will not be able to exchange data
with the server until the server has completed the current session. Figure 5.7 shows the sequence
diagram of two sessions conducted when client 2 attempts to connect to the server while client 1
is being served. Note that there is no interleaving of the sessions in this case.
Suppose each session can be expected to last ‘t’ time units and that ‘n’ clients have requested
connection at a given time. Disregarding other delays for the sake of simplicity, the next client
who requests connection can expect to be blocked for at least n*t time units. If t is large, the time
taken for the data to reach the destination will be unacceptable. The solution to this problem is to
introduce concurrency to the server, resulting in a concurrent server. A concurrent server is
capable of conducting multiple client sessions in parallel which can be provided using threads or
using asynchronous IPC operations.
Similar to iterative servers, there are concurrent servers, which use a single connection socket
to listen for connections. But a concurrent server creates a new thread to accept each connection
and to conduct a service session with the connected client; the thread is terminated upon the
conclusion of the session.
Figure 5.8 shows the sequence diagram for two concurrent sessions. With a concurrent server, a
client will not have to wait long for its connection to be accepted;
A stateless server is one that provides a service according to a stateless protocol and hence
does not have to maintain any state information, as in the case of the Daytime server or the
Echo server. A stateful server, as the name implies, is one that must maintain some state
information in order to provide its service.
Each time the server is contacted by a client, it increments the counter by 1 and sends the current
value of the counter to the client. To provide such a service, the server must have the value of the
counter placed in some storage where it can be retrieved and updated throughout the execution of
the server.
For a simple counter, the storage may be implemented using a variable of primitive data type in
Java. For more complex state information, the storage may be implemented using a file, a
database, or other mechanisms.
There are at least two schemes to maintain the session state data.
Stateless Server: In one scheme, the session state information may be maintained by the client
so that each request contains the session state information, allowing the server to service each
request in accordance with the state data sent in the request.
The dialog during a session will then proceed roughly as follows:
Client: Please send me block 1 of the file foo in directory someDir.
Server: Okay. Here is that block of the file.
Client: Please send me block 2 of the file foo in directory someDir.
Server: Okay. Here is that block of the file.
….
Client: Please send me block n of the file foo in directory someDir.
Server: Okay. Here is that block of the file.
By requiring the client to maintain the session data, this scheme allows the server to process each
request in the same manner, and thereby reduces the complexity of its application logic. Such a
server is known as a stateless server.
Stateful Server: In the second scheme, the session state information may be maintained by the
server, in which case the dialog during a session may proceed roughly as follows:
With this scheme, the server keeps track of the progress of the session by maintaining the session
state. Such a server is known as a stateful server. Figure 5.9 illustrates the difference between a
stateless server and a stateful server.
Fig 5.9 Difference between a stateless server and a stateful server
Stateful servers are more complex to design and implement. In addition to the logic required for
maintaining state data, provisions must be made to safeguard the state information in the event
that a session is disrupted. If a stateful server host fails temporarily in the midst of a session, it is
important for the ongoing session to resume in the correct state.
Another example of a stateful protocol is one for a shopping cart application. Each session must
maintain state data that keeps track of the identity of the shopper and the cumulative contents of
the shopping cart.