Block 5 ClientServer Model Jin Sa 11222020 Clientserver
Block 5: Client-Server Model Jin Sa 11/22/2020 Client-server Programming 1
Outline of block 5 • Client-server paradigm – Request-response protocol – Client-server applications and services • Basic client-server models using sockets – Connection-oriented servers vs connectionless servers • Software structure of network services – Daytime connectionless and connection-oriented – Echo connectionless and connection-oriented • Concurrent servers vs iterative servers • Stateful servers vs stateless servers 11/22/2020 Client-server Programming 2
Client-server paradigm 11/22/2020 Client-server Programming 3
Introduction • The Client-Server paradigm is the most popular model for distributed computing protocols. • It is the basis of all distributed computing paradigms at a higher level of abstraction. • It is service-oriented, and employs a request-response protocol. 11/22/2020 Client-server Programming 4
HTTP: An example of a requestresponse protocol • The Hypertext Transfer Protocol is a protocol for a process (the browser) to obtain a document from a web server process. • It is a request-response protocol: a browser sends a request to a web server process, which replies with a response. 11/22/2020 Client-server Programming 5
HTTP session • A web server process constantly listens for incoming request. • A web browser process makes a connection to the web server process, then issues a request. • The server processes the request and dispatches a response including the document requested by the browser. • The browser process then parses the response and displays the document. 11/22/2020 Client-server Programming 6
The Client-Server Paradigm • A server process, running on a server host, provides access to a service. • A client process, running on a client host, accesses the service via the server process. • The interaction of the processes proceeds according to a protocol. 11/22/2020 Client-server Programming 7
Examples of client-server applications • Well known Internet services include HTTP, FTP, DNS, etc. • User applications may also be built using the client-server paradigm. 11/22/2020 Client-server Programming 8
Another example: Daytime service Client: Hello, <client address> here. May I have a timestamp please. Server: Here it is: (time stamp follows) 11/22/2020 Client-server Programming 9
Basic client-server models using sockets 11/22/2020 Client-server Programming 10
Connectionless vs. connection-oriented server A connectionless server – Uses a connectionless IPC API (e. g. , connectionless datagram socket) – Sessions with multiple clients can be interleaved. A connection-oriented server – Uses a connection-oriented IPC API (e. g. streammode socket ) – Sessions with multiple clients can only be sequential unless the server is multi-threaded 11/22/2020 Client-server Programming 11
The Basic Connectionless Client. Server Model 11/22/2020 Client-server Programming 12
The Basic Connection-Oriented Client-Server Model 11/22/2020 Client-server Programming 13
Software Structure for a Network Service 11/22/2020 Client-server Programming 14
Software architecture • User interface layer: – On the server side, typically very simple. – On the client side, a user interface is normally needed. The user on the client side may request for services and receive responses from the server. In our example, we use simple text-mode user interface, more complicated GUI interface can be provided by the client process. • Application logic layer: what need to be done for this application. • Service layer: deals with details of IPC. 11/22/2020 Client-server Programming 15
Example: Daytime protocol • Connection-oriented using stream-mode • Connectionless using datagram 11/22/2020 Client-server Programming 16
Connection-oriented Daytime Client -server User interface: Daytime. Client 2 11/22/2020 Application logic: Daytime. Client. Helper 2 User interface/Application: Daytimeserver 2 Service: My. Stream. Socket Client-server Programming 17
Connection-oriented Daytime -- Client-side User interface • Daytime. Client 2. java provides the interface for a user of the client process. You will note that the code in this class is concerned with obtaining input (the server address), and displaying the output (the timestamp) to the user. • To obtain the timestamp, a method call to a “helper” class, Daytime. Client. Helper 2. java, is issued. This method hides the details of the application logic and the underlying service logic. In particular, the programmer of Daytime. Client 2. java need not be aware of which socket types is used for the IPC. 11/22/2020 Client-server Programming 18
Connection-oriented Daytime -- Client-side Application logic • Daytime. Cliet. Helper 2. java class performs the IPC for sending a request and receiving a response, using the My. Stream. Socket class. It provides one method called get. Timestamp. • Note that the details of using stream sockets are hidden from this class. In particular, this class does not need to deal with getting the streams. 11/22/2020 Client-server Programming 19
Connection-oriented Daytime -- Client-side service logic • My. Stream. Socket. java provides details of the IPC service. It is based on the stream-mode API. • My. Stream. Socket is a wrapper class. It contains an instance variable that refers to a Socket object. My. Stream. Socket provides for sending and receiving messages to and from a socket. 11/22/2020 Client-server Programming 20
Connection-oriented Daytime -- Server-side User interface and application logic • There is very little user interface on the server side. • The application logic is captured by Daytime. Server 2. java. The socket reference returned from accepting a connection request is used to instantiated a My. Stream. Socket object, whose send. Message method is used to send a timstamp to the client. 11/22/2020 Client-server Programming 21
Connection-oriented Daytime -- Server-side service logic • The same wrapper class My. Stream. Socket is used as for the client. 11/22/2020 Client-server Programming 22
Useful Java Stream-mode socket API (skip up to p 25) 11/22/2020 Client-server Programming 23
Key methods in the Server. Socket class • Server. Socket(int port) constructor • Socket accept() listens for a connection request and accepts it. A blocking operation. • void close() closes this server socket • void set. So. Timeout(int milliseconds) sets a timeout period to avoid infinite blocking 11/22/2020 Client-server Programming 24
Key methods in the Socket class • Socket(Inet. Address addr, int port) constructor • void close() closes the socket • Input. Stream get. Input. Stream() returns an input stream so that data may be read from the socket. A read operation on the Input. Stream is blocking. • Output. Stream get. Output. Stream() returns an output stream so that data may be written to the socket. A write operation on the Output. Stream is nonblocking. 11/22/2020 Client-server Programming 25
UML Diagram for stream mode Daytime Client User interface 11/22/2020 Application Client-server Programming Service 26
UML Diagram for stream mode Daytime Server User interface/ Service Application 11/22/2020 Client-server Programming 27
Source code • 2005 -6/Code/Client. Server – Daytime. Server 2 – Daytime. Clienthelper 2 – My. Stream. Socket 11/22/2020 Client-server Programming 28
Connectionless Daytime Client-server User interface: Daytime. Client 1 11/22/2020 Application logic: Daytime. Client. Helper 1 User interface/Application: Daytimeserver 1 Service: My. Datagram. Socket Datagram. Message Client-server Programming 29
Connectionless Daytime -- Client-side User interface • Daytime. Client 1. java provides the interface for a user of the client process. You will note that the code in this class is concerned with obtaining input (the server address), and displaying the output (the timestamp) to the user. • To obtain the timestamp, a method call to a “helper” class, Daytime. Client. Helper 1. java, is issued. This method hides the details of the application logic and the underlying service logic. In particular, the programmer of Daytime. Client 1. java need not be aware of which socket types is used for the IPC. 11/22/2020 Client-server Programming 30
Connectionless Daytime -- Client-side Application logic • The Daytime. Client. Helper 1. java class performs the IPC for sending a request and receiving a response, using a specialized class of the Datagram. Socket, My. Datagram. Socket. • Note that the details of using datagram sockets are hidden from this class. In particular, this class does not need to deal with the byte array for carrying the payload data. 11/22/2020 Client-server Programming 31
Connectionless Daytime -- Client-side Service logic • The My. Client. Datagram. java class provides the details of the IPC service, in this case using the datagram socket API. 11/22/2020 Client-server Programming 32
Connectionless Daytime client program • In 2005_6/Code/Client. Server: My. Datagram. Socket. java Daytime. Client. Helper 1. java Daytime. Client 1. java 11/22/2020 Client-server Programming 33
Connectionless Daytime – Server side user interface/application • User interface: Typically, there is very little user logic on the server-side. • Application logic: The Daytime. Server 1. java class encapsulates the server-side application logic. This module executes in a forever loop, waiting for a request from a client and then conduct a service session for that client. The module performs the IPC for receiving a request and sending a response, using a specialized class of the Datagram. Socket, my. Datagram. Socket. Note that the details of using datagram sockets are hidden from this module. In particular, this module does not need to deal with the byte array for carrying the payload data. 11/22/2020 Client-server Programming 34
Connectionless Daytime –Server side service logic • The My. Server. Datagram. java class provides the details of the IPC service, in this case using the datagram socket API. • The Datagram. Message. java class is used to group together the message from the client and the address of the client. 11/22/2020 Client-server Programming 35
Connectionless Daytime server program • In Code/Client. Server: My. Datagram. Socket. java Datagram. Message. java Daytimeserver 1. java 11/22/2020 Client-server Programming 36
Useful Java Datagram socket API (skip until p 41) 11/22/2020 Client-server Programming 37
Key Methods and Constructors for Datagram -- Datagram. Socket • Datagram. Socket() constructor, used if no need to receive datagram • Datagram. Socket(int port) constructor, used if need to receive packet. The port number can then be specified in the datagram packet sent by the other process. • void close() to close the datagram socket 11/22/2020 Client-server Programming 38
Key Methods and Constructors for Datagram-- Datagram. Socket • void receive(Datagram. Packet p) to receive a datagram packet, p will refer to the packet • void send(Datagram. Packet p) to send the datagram packet pointed by p using this datagram socket • void set. So. Timeout(int timeout) Sets a timeout period for the blocking receive operations 11/22/2020 Client-server Programming 39
Key Methods and Constructors for Datagram -- Datagram. Packet • Datagram. Packet(byte[] buf, int l) constructor for receiving packets of length l, data received will be stored in buf • Datagram. Packet(byte[] buf, int l, Inet. Address ip. Address, int port) constructor for sending packets to the socket bound to the port number on the host. 11/22/2020 Client-server Programming 40
Key Methods and Constructors for Datagram -- Datagram. Packet • public Inet. Address get. Address( ) Return the IP address of the (remote) host from a socket of which the datagram packet was received. • public int get. Port( ) Return the port number on the (remote) host from a socket of which the datagram packet was received. 11/22/2020 Client-server Programming 41
UML Diagram for the Datagram Daytime Client Presentation 11/22/2020 application Client-server Programming service 42
UML Diagram for the Datagram Daytime server Presentation/ Application 11/22/2020 service Client-server Programming 43
Advantages of separating the layers of logic It allows each module to 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 logic and the service logic may focus on developing the other modules. 11/22/2020 Client-server Programming 44
Advantages of separating the layers of logic The separation allows modifications to be made to the logic at the user interface without requiring changes to be made at the lower layers. For example, the user interface can be changed from text-mode to graphical without requiring any change be made to the application logic or the service logic. Likewise, changes made in the service logic should not affect the presentation logic, e. g. Daytime. Client 1 and Daytime. Client 2 are exactly the same even though the underlying communication mechanisms are very different. 11/22/2020 Client-server Programming 45
Example 2: Echo • Connectionless using datagram (skip) • Connection-oriented using stream-mode socket • Concurrent echo server 11/22/2020 Client-server Programming 46
Connectionless Echo Client-server (skip) Presentation: Echo. Client 1 11/22/2020 Application: Echo. Client. Helper 1 Presentation/Application: Echoserver 1 Service: My. Datagram. Socket Datagram. Message Client-server Programming 47
Connectionless Echo -- Client side presentation logic (skip) • The presentation logic is encapsulated in Echo. Client 1. java. • It provides the user interface. It uses a loop for the text lines to send to the server. • The sending of the text string and receiving of the echoed text is handled by a method, get. Echo of the Echo. Client. Help 1 class. 11/22/2020 Client-server Programming 48
Connectionless Echo -- Client side application logic (skip) • The Echo. Client. Helper 1 class provides the application logic for the client. • An instance of this class is created for each client process. It holds the address of the server and a reference to a socket used by the client for IPC. • The get. Echo method uses the socket to send a line and then receive a line from the server. 11/22/2020 Client-server Programming 49
Connectionless Echo -- Client side service logic (skip) • This is provided by My. Datagram. Socket as in the case of the Daytime example. 11/22/2020 Client-server Programming 50
Connectionless Echo -- Server side user interface/application logic (skip) • Echo. Server 1. java combines the presentation and application logic for the server. • In each iteration of the loop, the server reads a line from the socket and then writes a line back to the socket, addressing the reply to the sender. • Since there is no connection involved, it is possible for the server to interact with different clients in successive interactions, resulting in interleaved concurrent service session. 11/22/2020 Client-server Programming 51
Connectionless Echo -- Server side service logic (skip) • This is again the same as in the case of the Daytime example. The two classes My. Datagram. Socket and Datagram. Message are used to provide service level logic. 11/22/2020 Client-server Programming 52
Implementation of Echo 1(skip) • • • Echo. Client 1. java Echo. Client. Helper 1. java My. Datagram. Socket. java Echo. Server 1. java Datgram. Message. java Check the Datagram socket API on page 39. 11/22/2020 Client-server Programming 53
Connection-oriented Echo Clientserver User interface: Echo. Client 2 11/22/2020 Application: Echo. Client. Helper 2 User interface/Application: Echoserver 2 Service: My. Stream. Socket Client-server Programming 54
Connection-oriented Echo -- client side user interface logic • Echo. Client 2. java provides the user interface. It uses a loop for the text lines to send to the server. • The sending of the text string and receiving of the echoed text is handled by a method, get. Echo of the Echo. Client. Help 2 class. 11/22/2020 Client-server Programming 55
Connection-oriented Echo -- client side application logic • This is encapsulated in Client. Helper 2. java. • The connection to the server is made in the constructor. • Each round of message exchanged is provided by the method get. Echo. 11/22/2020 Client-server Programming 56
Connection-oriented Echo -- client side service logic • This is in My. Stream. Socket. 11/22/2020 Client-server Programming 57
Connection-oriented Echo -- server side user interface/application logic • In Echo. Server 2, a server socket is first created to listen and to accept connections. • For each connection accepted, the server continually receives a message and echoes it via the data socket attached to the connection. • At the end of the session, the data socket for the current connection is closed. The connection is terminated. • The server then waits to accept another connection. 11/22/2020 Client-server Programming 58
Connection-oriented Echo -- server side service logic • Again in My. Stream. Socket. 11/22/2020 Client-server Programming 59
Notes on the sequential nature • In the Daytime and the Echo example, we have seen that 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 new client will not be able to exchange data with the server until the server has completed the current session. • Such a server is called an iterative server. 11/22/2020 Client-server Programming 60
Concurrent Server 11/22/2020 Client-server Programming 61
Concurrent, Connection-Oriented Server • The Echo server services one client at a time. • If the duration of each client session is significant, then the latency or turnaround time of a client request becomes unacceptable if the number of concurrent client processes is large. 11/22/2020 Client-server Programming 62
Concurrent, connection-oriented server • To improve the latency, a server process spawns a child process or child thread to process the protocol for each client. Such a server is termed a concurrent server, compared to an iterative server. 11/22/2020 Client-server Programming 63
Concurrent, connection-oriented server • A concurrent server can be implemented using multiple threads. • A concurrent server uses its main thread to accept connections, and spawns a child thread to process the protocol for each client. • Clients queue for connection, then are served concurrently. The concurrency reduces latency significantly. 11/22/2020 Client-server Programming 64
Connection-oriented server: latency analysis (skip) • For a given server S, let Tc be the expected time that S takes to accept a connection, and Tp be the expected time S takes to process the protocol for a client. • Further assume that the expected number of concurrent clients requiring the service of S is N. 11/22/2020 Client-server Programming 65
11/22/2020 Client-server Programming 66
Concurrent Echo server • Similar to an iterative server, a concurrent echo server uses a single server socket to listen for connections. • But the concurrent server creates a new thread to conduct a service session with each new client. • This frees the server to handle connection requests from other clients. • Once a service session with a client is finished, the thread is terminated. 11/22/2020 Client-server Programming 67
Echo. Server 3 • In Echo. Server 3, once a connection is accepted, the socket associated with the connection is passed to a thread -- an instance of Echo. Server. Thread. • The thread is started. • The server is now ready to accept another connection request. 11/22/2020 Client-server Programming 68
Echo. Server. Thread • The Echo. Server. Thread class extends Thread. • Each Thread class has a run method. It defines the behaviour of the thread. • The run method in Echo. Server. Thread receives a message from a client; it checks to see if it is an end of session indicator. If it is close the socket, and terminates the thread. If it is not end of the session, it sends the message back to the client. 11/22/2020 Client-server Programming 69
Server Thread class template class Server. Thread extends Thread { My. Stream. Socket my. Data. Socket; Server. Thread(My. Stream. Socket my. Socket) { my. Data. Socket = my. Socket; } public void run( ) { try { //add code here }// end try catch (Exception ex) { System. out. println("Exception caught: " + ex); } } //end run } //end class 11/22/2020 Client-server Programming 70
Concurrent Echo Server programs See Echo. Server 3. java See Echo. Server. Thread. java 11/22/2020 Client-server Programming 71
Stateful Server 11/22/2020 Client-server Programming 72
State Information • There are two types of state information: Global state information and session state information 11/22/2020 Client-server Programming 73
Global State Information • Information maintained by the server for all the clients throughout the lifetime of the server. • Example: some kind of counter protocol. Each time a server is contacted by a client, the counter is incremented. 11/22/2020 Client-server Programming 74
Session State Information • For some protocols or applications, a server must maintain information specific to a client during its service session. • Example: FTP. 11/22/2020 Client-server Programming 75
Examples of Session State Information With a protocol such as ftp, there is a need for the server to keep track of the progress of the session, such as the name of the file and 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. 11/22/2020 Client-server Programming 76
11/22/2020 Client-server Programming 77
Examples of Session State Information Another example of a stateful protocol is one for a shopping cart application. Each session must maintain state data that keeps track of the identifier of the shopper and the cumulative contents of the shopping cart. 11/22/2020 Client-server Programming 78
Stateful vs. Stateless server • Stateless server is straightforward to code, but leaves the complexity to the clients. • Stateful server is harder to code, but the state information maintained by the server can reduce the data exchanged, and thereby the response time and allows enhancements to a basic service. • Maintaining stateful information is difficult in the presence of failures. 11/22/2020 Client-server Programming 79
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. 11/22/2020 Client-server Programming 80
Example of a stateful server • Consider a counter protocol (not a standard internet protocol), it requires the server to maintain a global state information, a counter. • The counter is initialised to 0. • Each time the server is contacted by a client, the counter is incremented; the current value of the counter is sent back to the client. • To provide such a service, the server keeps the counter in some storage where it can be retrieved and updated throughout the life time of the server. 11/22/2020 Client-server Programming 81
Connection-oriented counter server • Server side – Counter. Server 2 – My. Stream. Socket • Client side – Counter. Client 2 – Counter. Client. Helper 2 – My. Stream. Socket 11/22/2020 Client-server Programming 82
Key statements in Counter. Server 2 Server. Socket connection. Socket = new Server. Socket(server. Port); while (true) { My. Stream. Socket my. Data. Socket = new My. Stream. Socket(connection. Socket. accept()); my. Data. Socket. receive. Message(); increment( ); my. Data. Socket. send. Message(" "+counter); } //end while 11/22/2020 Client-server Programming 83
Key Statements in Counter. Client 2 String host. Name = "localhost"; int port. Num = 12345; System. out. println( Counter. Client. Helper 2. get. Counter( host. Name, port. Num)); 11/22/2020 Client-server Programming 84
Key Statements in Counter. Client. Helper 2 public static int get. Counter(String host. Name, int port. Num){ … … Inet. Address server. Host = Inet. Address. get. By. Name(host. Name); My. Stream. Socket my. Data. Socket= new My. Stream. Socket(server. Host, port. Num); my. Data. Socket. send. Message(""); message = my. Data. Socket. receive. Message(); counter = Integer. parse. Int(message. trim()); } 11/22/2020 Client-server Programming 85
Connectionless Implementation (skip upto 89) • Server side – Counter. Server 1 – My. Stream. Socket • Client side – Counter. Client 1 – Counter. Client. Helper 1 – My. Stream. Socket 11/22/2020 Client-server Programming 86
Counter. Server 1 • This implementation is based on the connectionless datagram model. • At the presentation and application level, a datagram socket is created with a port number. • A datagram message is received from a client. • The datagram message consists of the client’s address and port number. • The server increment the counter. • It extract out the client’s address and port number. • It then send a message to the client via the socket with the new value of the counter. 11/22/2020 Client-server Programming 87
Counter. Client 1 • At the presentation layer, Counter. Client 1 simply presents a simple interface for displaying the counter value. 11/22/2020 Client-server Programming 88
Counter. Client. Helper 1 At the application level • Counter. Client. Help 1 creates a socket. • An empty message is sent to the server so that the server is able to identify the client’s address and port number. • It then receives a message from the server. • The message should represent the counter value. 11/22/2020 Client-server Programming 89
Another example: Count. Down • Each client contacts the server by sending an integer n • The client then loops to receive messages from the server bearing the values n, n-1, n-2, …, 0 successively. 11/22/2020 Client-server Programming 90
Count. Down • Stateful server? • Global state or session state? Server side: Count. Down. Server Client side: Count. Down. Client. Helper 11/22/2020 Client-server Programming 91
Key statements in Count. Down. Server. Socket my. Connection. Socket = new Server. Socket(server. Port); while (true) { My. Stream. Socket my. Data. Socket = new My. Stream. Socket(my. Connection. Socket. accept( )); String message=my. Data. Socket. receive. Message(); //session state information for each client int client. Counter = Integer. parse. Int(message. trim()); while (client. Counter >=0) { my. Data. Socket. send. Message(" "+client. Counter); System. out. println("client's counter is: “ +client. Counter); client. Counter--; } //end while 11/22/2020 Client-server Programming 92
Key statements in Count. Down. Client String host. Name = "localhost"; int port. Num = 12333; Count. Down. Client. Helper appl= new Count. Down. Client. Helper(host. Name, port. Num); Scanner in=new Scanner(System. in); System. out. println("Enter a number"); int n=in. next. Int(); appl. counting. Down(n); 11/22/2020 Client-server Programming 93
Key statements in Count. Down. Client. Helper public void counting. Down(int n) { my. Data. Socket. send. Message(""+n); boolean done = false; String message; int my. Counter; while (!done) { message=my. Data. Socket. receive. Message(); System. out. println("message from the server is “ +message); my. Counter=Integer. parse. Int(message. trim()); if (my. Counter==0) done = true; }//while 11/22/2020 Client-server Programming 94
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 server, and a file from a file server. 11/22/2020 Client-server Programming 95
Summary • The software architecture of network applications: user interface, application logic, and service logic. • Connectionless server versus connectionoriented 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. 11/22/2020 Client-server Programming 96
Homework • Review this block, highlight issues that you think are important. For example: – What are the advantages of structuring the clientserver software architecture in a layers approach? – Describe the basic client-server connectionless model and connection-oriented model. – What is a concurrent server compared with an interactive server? – What is a stateful server? What is global state information? What is session station information? 11/22/2020 Client-server Programming 97
Homework • Download the source code, and run them. • Modify these code by introducing more sophisticated user interfaces. • You have seen a number of different sets of classes, e. g. Day. Time. Client 1, Day. Time. Server 1 and Day. Time. Client 2, Day. Time. Server 2. In your own words, describe the difference between these two sets of classes. • Implement a concurrent counter server based on the stream mode communication. • Add a counter to Echo. Server 3 to record the number of clients the server has granted connections. • Add another counter to Echo. Server 3 to represent the number of currently connected clients. 11/22/2020 Client-server Programming 98
- Slides: 98