0% found this document useful (0 votes)
29 views

The Client Server Model Part2 - M Liu

The document discusses connectionless and connection-oriented servers, with connectionless servers able to handle interleaved client sessions and connection-oriented servers requiring sequential or threaded handling of sessions. It provides examples of iterative and concurrent connection-oriented echo servers in pseudocode to illustrate the concepts.

Uploaded by

pfidal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

The Client Server Model Part2 - M Liu

The document discusses connectionless and connection-oriented servers, with connectionless servers able to handle interleaved client sessions and connection-oriented servers requiring sequential or threaded handling of sessions. It provides examples of iterative and concurrent connection-oriented echo servers in pseudocode to illustrate the concepts.

Uploaded by

pfidal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

The Client-Server Model – part 2

M. L. Liu

Distributed Computing, M. Liu 1


Connectionless server vs.
connection-oriented server

Distributed Computing, M. Liu 2


Connectionless vs. connection-
oriented server
A connectionless server
– Uses a connectionless IPC API (e.g., connectionless
datagram socket)
– Sessions with concurrent clients can be interleaved.
A connection-oriented server
– Uses a connection-oriented IPC API (e.g. stream-
mode socket )
– Sessions with concurrent clients can only be
sequential unless the server is threaded

Distributed Computing, M. Liu 3


EchoServer 1(connectionless) Excerpt
public class EchoServer1 {
public static void main(String[] args) {

// instantiates a datagram socket for both sending
// and receiving data
MyServerDatagramSocket mySocket = new
MyServerDatagramSocket(serverPort);
while (true) { // forever loop
DatagramMessage request =
mySocket.receiveMessageAndSender();
String message = request.getMessage( );
mySocket.sendMessage(request.getAddress( ),
request.getPort( ), message);
} //end while
}

Distributed Computing, M. Liu 4


Concurrent client sessions with
EchoServer1
Ech o S e rv e r1 clie n t 1 clie n t2
m e ssage
e ch o
m e ssage
e ch o
m e ssage
e ch o

m e s s ag e
e ch o
m e s s ag e

e ch o

Distributed Computing, M. Liu 5


EchoServer2 (Connection-oriented)
excerpt
ServerSocket myConnectionSocket = new ServerSocket(serverPort);
while (true) { // forever loop
MyStreamSocket myDataSocket = new MyStreamSocket
(myConnectionSocket.accept( ));
boolean done = false;
while (!done) {
message = myDataSocket.receiveMessage( );
if ((message.trim()).equals (endMessage)){
myDataSocket.close( );
done = true;
} //end if
else {
myDataSocket.sendMessage(message);
} //end else
} //end while !done
} //end while forever

Distributed Computing, M. Liu 6


Two consecutive client sessions with echo server2

Ech o S e rv e r2 clie n t 1 clie n t2


m e ssage
e ch o
m e ssage
e ch o
m e ssage
e ch o

m e s s ag e
e ch o
m e s s ag e

e ch o

Distributed Computing, M. Liu 7


Iterative servers
Vs.
Concurrent servers

Distributed Computing, M. Liu 8


Concurrent Server
A connection-oriented server can be
threaded so that it can service multiple
clients concurrently. Such a server is said
to be a concurrent server.
 An unthreaded connection-oriented server
is said to be an iterative server.

Distributed Computing, M. Liu 9


A Concurrent, Connection-
oriented Server
A c lien t p r o c es s at th e h ead o f th e c o n n ec tio n q u eu e
s e rv e r h o s t

c o n c u r r en t s er v er
p r o c es s
th e s er v er c o n n ec tio n q u eu e
s erv ic e

th e m ain th r ead ac c ep ts c o n n ec tio n s

a c h ild th read p r o c es s es
th e p ro to c o l f o r a A c lien t p ro c es s w h o s e c o n n ec tio n h as b een ac c ep ted
c lien t p ro c es s
A c lien t p r o c es s w h o s e c o n n ec tio n h as b een ac c ep ted

Distributed Computing, M. Liu 10


Sequence diagram – EchoServer3
E c h o Se r v e r 3 E c h o C lie n t 1
E c h o C lie n t 2
E c h o Se r v e r 3
t h read 1

E c h o Se r v e r 3
t h read 2

ac c ep t
c o n n ec tio n

Distributed Computing, M. Liu 11


EchoServer3 (concurrent) excerpt
ServerSocket myConnectionSocket =
new ServerSocket(serverPort);
while (true) { // forever loop
MyStreamSocket myDataSocket = new
MyStreamSocket
(myConnectionSocket.accept( ));
Thread theThread =
new Thread(new
ServerThread(myDataSocket));
theThread.start();
} //end while forever
Distributed Computing, M. Liu 12
ServerThread.java excerpt
class ServerThread implements Runnable {
static final String endMessage = ".";
MyStreamSocket myDataSocket;
EchoServerThread(MyStreamSocket myDataSocket) {
this.myDataSocket = myDataSocket;
}
public void run( ) {
boolean done = false; String message;
try { // put in here the logic for each client session
while (!done) {
message = myDataSocket.receiveMessage( );
if ((message.trim()).equals (endMessage)){
myDataSocket.close( ); done = true;
} //end if
else {
myDataSocket.sendMessage(message);
} //end else
} //end while !done
}// end try
catch (Exception ex) {
}
} //end run
} //end class

Distributed Computing, M. Liu 13


Echo3Server concurrent sessions
Ech o S e rv e r3 clie n t 1 clie n t2
m e ssage m e ssage
e ch o e ch o
m e ssage
m e ssage
e ch o
e ch o
m e ssage
e ch o

Distributed Computing, M. Liu 14


Server Thread class template
class ServerThread implements Runnable {
static final String endMessage = ".";
MyStreamSocket myDataSocket;
ServerThread(MyStreamSocket myDataSocket) {
this.myDataSocket = myDataSocket;
}
public void run( ) {
boolean done = false;
String message;
try {
//add code here
}// end try
catch (Exception ex) {
System.out.println("Exception caught in thread: " + ex);
}
} //end run
} //end class

Distributed Computing, M. Liu 15


Stateful servers vs. Stateless
servers

Distributed Computing, M. Liu 16


Session State Information
For some protocols or applications, a server
must maintain information specific to a client
during its service session.
Consider a network service such as file transfer.
A file is typically transferred in blocks,
requiring several rounds of data exchanges to
complete the file transfer. The dialog during a
session proceeds roughly as follows:
Client: Please send me the file foo in directory someDir.
Server: Okay. Here is block 1of the file
Client: Got it.
Server. Okay. Here is block2 of the file
Client: Got it.

Server. Okay. Here is block3 of the file
Client: Got it.

Distributed Computing, M. Liu 17


Stateful server
 A stateful server maintain stateful information on each
active client.
 Stateful information can reduce the data exchanged, and
thereby the response time.
FTP s e r ve r FTP s e r ve r

FTP C l i e n t
FTP C l i e n t

f ile I D f ile p o sit io n


f ile I D

f ile p o sit io n

G E T f ile n a m e
G E T f ile n a m e
f ile I D
read y
s e n d < f ile I D > , b lo c k 0
s e n d n ex t b lo c k
d a ta f r o m b lo c k 0 o f f ile
d a ta f r o m b lo c k 0 o f f ile
s e n d < f ile I D > , b lo c k 1
s e n d n e x t b lo c k
d a ta f r o m b lo c k 1 o f f ile
d ata f r o m b lo c k 1 o f f ile
... ...
Distributed Computing, M. Liu 18
Stateful vs. Stateless server
 Stateless server is straightforward to code.
 Stateful server is harder to code, but the state information
maintained by the server can reduce the data exchanged,
and allows enhancements to a basic service.
 Maintaining stateful information is difficult in the presence
FTP s e r ve r

of failures. f ile I D f ile p o sit io n


FTP C l i e n t

G E T f ile n a m e
r ead y
s en d n ex t b lo c k
d ata f r o m b lo c k 0 o f f ile
d ata is lo s t d u e to n etw o r k f a ilu r e
s en d n ex t b lo c k
c lien t r es u b m its r eq u es t
d ata f r o m b lo c k 1 o f f ile
c lien t r ec eiv es d ata as b lo c k 0 o f f ile;
... th e tr u e b lo c k 0 is m is s ed .

Distributed Computing, M. Liu 19


Session State Information - 2
With a protocol such as ftp, there is a need for the
server to keep track of the progress of the session,
such as which block of the file needs to be fetched
next. A server does so by maintaining a set of
state for each session, known as the session state
data. For the file transfer protocol, the session
state data may include the name of the file being
transferred, and the current block count.
Another example of a stateful protocol is one for a
shopping cart application. Each session must
maintain state data that keeps track of the
identify of the shopper and the cumulative
contents of the shopping cart.

Distributed Computing, M. Liu 20


State Data Storage
In our example, the state data – the sleep time
interval - is stored in a local variable in the run
method of each thread. Since each client is
serviced by a separate thread, the local
variable suffices as a storage for the state
data.
Using local variables in a thread to store session
state data is adequate for a network service
server. In complex network applications such
as shopping carts, more complex mechanisms
are needed for state data storage.

Distributed Computing, M. Liu 21


Stateful vs. stateless server

 In actual implementation, a server may be


– Stateless
– Stateful
– A hybrid, wherein the state data is distributed
on both the server-side and the client-side.

 Which type of server is chosen is a design


issue.

Distributed Computing, M. Liu 22


A client can contact multiple
servers
A process may require the service of multiple
servers. For example, it may obtain a
timestamp from a daytime server, data from a
database Sserver,
1 andS 2 a file from
S 3 a file server.

C lien t

Distributed Computing, M. Liu 23


Summary
You have been introduced to the client-server paradigm in
distributed computing. Topics covered include:
 The difference between the client-server system architecture and the
client-server distributed computing paradigm.
 Definition of the paradigm and why it is widely adopted in network
services and network applications.
 The issues of service sessions, protocols, service location, interprocess
communications, data representation, and event synchronization in the
context of the client-server paradigm.
 The three-tier software architecture of network applications:
Presentation logic, application logic, and service logic.
 Connectionless server versus connection-oriented server.
 Iterative server versus concurrent server and the effect on a client
session.
 Stateful server versus stateless server.
 In the case of a stateful server: global state information versus session
state information.

Distributed Computing, M. Liu 24

You might also like