310 likes | 966 Views
The Client-Server Paradigm. Introduction. The Client-Server paradigm is the most prevalent 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 .
E N D
Introduction • The Client-Server paradigm is the most prevalent 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.
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 process proceeds according to a protocol.
Client-server applications and services • An application based on the client-server paradigm is a client-server application. • On the Internet, many services are Client-server applications. These services are often known by the protocol that the application implements. • Well known Internet services include HTTP, FTP, DNS, finger, gopher, etc. • User applications may also be built using the client-server paradigm.
Connectionless Server A connectionless server accepts one request at a time from any client, processes the request, and sends the response to the requestor.
Connection-Oriented Client-Server applications • A client-server application can be either connection-oriented or connectionless. • In a connection-oriented client-server application: • The server is passive: it listens and waits for connection requests from clients, and accepts one connection at a time. • A client issues a connection request, and waits for its connection to be accepted. • Once a server accepts a connection, it waits for a request from the client. • When a client is connected to the server, it issues a request and waits for the response. • When a server receives a request, it processes the request and sends a response, then wait for the next request, if any. • The client receives the request and processes it. If there are further requests, the process repeats itself until the protocol is consumated.
Connectionless Echo Server DatagramSocket ds = new DatagramSocket(port); while (true) { try { // create a new datagram packet byte[] buffer = new byte[MAXLEN]; DatagramPacket dp = new DatagramPacket(buffer, MAXLEN); ds.receive(dp); len = dp.getLength(); cAddr = dp.getAddress(); cPort = dp.getPort(); String s = new String(dp.getData(), 0, len); System.out.println(dp.getAddress() + " at port " + dp.getPort() + " says " + s); // create a datagram packet to send to client DatagramPacket theEcho = new DatagramPacket(buffer,len, cAddr, cPort); ds.send(theEcho); } // end try … } // end while
Concurrent, Connection-Oriented Server • A connection-oriented 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. • 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.
Concurrent, connection-oriented server - 2 • 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.
Connection-oriented server: latency analysis 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.
Connection-oriented Daytime Server …theServer = new ServerSocket(thePort); p = new PrintWriter(System.out); try { p.println("Echo Server now in business on port " + thePort ); p.flush(); theConnection = theServer.accept(); // read a line from the client theInputStream = new BufferedReader (new InputStreamReader (theConnection.getInputStream())); p = new PrintWriter(theConnection.getOutputStream()); while (!done){ theLine = theInputStream.readLine(); if (theLine == null) done = true; else{ p.println(theLine); p.flush(); } } theConnection.close(); Connection acceptance Protocol processing
theServer = new ServerSocket(thePort); p = new PrintWriter(System.out); try { p.println("dayTime Server now in business on port " + thePort ); p.flush(); while (true) { theConnection = theServer.accept(); daytimeServerThread theThread = new daytimeServerThread(theConnection); theThread.start(); } } public class daytimeServerThread extends Thread { Socket theConnection; public daytimeServerThread(Socket s) { theConnection = s; } public void run() { try { PrintWriter p; p = new PrintWriter(theConnection.getOutputStream()); p.println(new Date()); p.flush(); theConnection.close(); } catch (IOException e) { System.err.println(e); } } // end try } // end thread class Connection-oriented Concurrent DayTime Server ConcurrentDaytimeServer.javaDaytimeServerThread.java
public class echoServer { public static void main(String[] args) { ServerSocket theServer; int thePort; Socket theConnection; PrintWriter p; BufferedReader theInputStream; String theLine; boolean done = false; … theServer = new ServerSocket(thePort); p = new PrintWriter(System.out); try { theConnection = theServer.accept(); // read a line from the client theInputStream = new BufferedReader (new InputStreamReader (theConnection.getInputStream())); p = new PrintWriter(theConnection.getOutputStream()); while (!done){ theLine = theInputStream.readLine(); if (theLine == null) done = true; else { p.println(theLine); p.flush(); } // end if } //end while theConnection.close(); } // end try … Connection-oriented Echo Server
Concurrent Echo Server See ConcurrentEchoServer.java See EchoServerThread.java
Stateful server • A stateful server maintain stateful information on each active client. • Stateful information can reduce the data exchanged, and thereby the response time.
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 of failures.
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.
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.
Middleware A process can serve as a intermediary, or middleware, between a client and a server.