Presentation is loading. Please wait.

Presentation is loading. Please wait.

Client-server Programming

Similar presentations


Presentation on theme: "Client-server Programming"— Presentation transcript:

1 Client-server Programming
Block 7: Socket 2 Jin Sa 3-Aug-18 Client-server Programming

2 Client-server Programming
Outline of Block 7 Iterative server Concurrent server Stateful server Developing an example 3-Aug-18 Client-server Programming

3 Client-server Programming
Iterative server Version 1: Acceptor only accept connection request once. Not very useful server. Server tends to be “always-on”. Version 2: Modify Acceptor so that it can accept connection requests from many clients 3-Aug-18 Client-server Programming

4 Client-server Programming
Acceptor (Server) import java.net.*; import java.io.*; public class Acceptor { public static void main(String[] args) { try { // Create a serverSocket // listen for connection request and create a data socket -- // see slide Acceptor (1) // get an output stream for writing to the data socket – // see slide Acceptor (2) // write a message into the data stream – // see slide Acceptor (3) // close the data socket and // close the serversocket – // see slide Acceptor (4) } // end try catch (Exception ex) { ex.printStackTrace( ); } //end catch } // end main } // end class 3-Aug-18 Client-server Programming

5 AccporMore: example of an iterative server
Use a single serverSocket to connection request Use a loop to process the connection requests. Once the server has finished processing each client request, it is ready to accept another client connection request. 3-Aug-18 Client-server Programming

6 AcceptorMore (iterative server)
import java.net.*; import java.io.*; public class AcceptorMore { public static void main(String[] args) { try { // Create a serverSocket //loop for each connection request while (true){ // listen for connection request and create a data socket // get an output stream for writing to the data socket // write a message into the data stream // close the data socket and } // close the serversocket – } // end try catch (Exception ex) { ex.printStackTrace( ); } //end catch } // end main } // end class 3-Aug-18 Client-server Programming

7 Issues with iterative server
The AcceptorMore server services one client at a time. If the duration of each client session is significant, then the turnaround time of a client request becomes unacceptable if the number of concurrent client processes is large. 3-Aug-18 Client-server Programming

8 Client-server Programming
Concurrent server (1) To improve the turnaround time, 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. 3-Aug-18 Client-server Programming

9 Client-server Programming
Concurrent server (2) 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. 3-Aug-18 Client-server Programming

10 AcceptorConc: example of a concurrent server
Similar to an iterative server, a concurrent Acceptor 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. 3-Aug-18 Client-server Programming

11 Client-server Programming
AcceptorThread The AcceptorThread class extends Thread. Each Thread class has a run method. It defines the behaviour of the thread. 3-Aug-18 Client-server Programming

12 AcceptorThread template
public class AcceptorThread extends Thread { private Socket dataSocket; public AcceptorThread(Socket mySocket) { dataSocket=mySocket; } public void run(){ //behaviour of the tread 3-Aug-18 Client-server Programming

13 AcceptorConc (concurrent server)
public class AcceptorConc { public static void main(String[] args) { try { // Create a serverSocket //loop for each connection request while (true) { // listen for connection request and create a data socket AcceptorThread aThread = new AcceptorThread(dataSocket); aThread.start(); } // close the serversocket } // end try 3-Aug-18 Client-server Programming

14 Iterative server vs Concurrent server
Trivial to implement. Easy to serialize accesses to a central database. Server is locked while dealing with a request. If the request does take longer than allowed, no other clients can get service. Concurrent server Individual client requests can be of any complexity and duration Complexity inherent in concurrent processing. 3-Aug-18 Client-server Programming

15 Client-server Programming
Stateful server The examples so far are stateless servers, i.e. The server handles the client request, sends back the response, the connection is dropped. No need to keep any state information to relate different connections from the same client. However many applications need to relate different connections from the same clients, e.g. file transfer, shopping basket. Server needs to keep some state information to decide on the correction actions, e.g. knowing which block of the file to send. 3-Aug-18 Client-server Programming

16 An example of stateful server
In Sun’s tutorial on sockets, it has a server that serves up the “Knock-knock” jokes. Server: "Knock knock!" Client: "Who's there?" Server: “Turnip." Client: “Turnip who?" Server: “Turnip the heat, it’s cold here.“ The server needs to know where the client and the server are in their conversation in order to serves up the proper response. The server keeps track the current state (sent knock knock, sent clue, and so on), and returns the various text pieces of the joke depending on the current state. 3-Aug-18 Client-server Programming

17 Stateful vs Stateless Servers
Client connects to the server Client makes one request Server handles request Client and server drop connection Stateful Server Server handles request C Client makes another request Server handles request etc. Client and server drop connection when done Server needs to remember information about the client while the client is connected - the state of the connection Stateful servers are much more complicated than stateless servers 3-Aug-18 Client-server Programming

18 Developing a simple application using TCP socket - scenario
A client reads a line from keyboard and sends to the server The server reads a line from its socket. The server converts the line to uppercase. The server sends the modified line back to client The client reads the line and displays it. 3-Aug-18 Client-server Programming

19 Example: Java server (TCP)
import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String responseSentence; while(true) { Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket 3-Aug-18 Client-server Programming

20 Example: Java server (TCP), cont
Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection 3-Aug-18 Client-server Programming

21 Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; Create scanner Create client socket, connect to server Create output stream attached to socket 3-Aug-18 Client-server Programming

22 Example: Java client (TCP), cont.
Create input stream attached to socket Send line to server Read line from server 3-Aug-18 Client-server Programming

23 Example: Java server (TCP)
import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String responseSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket dataSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(dataSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket 3-Aug-18 Client-server Programming

24 Example: Java server (TCP), cont
PrintWriter outToClient = new PrintWriter( new OutputStreamWriter( dataSocket.getOutputStream())); clientSentence = inFromClient.readLine(); responseSentence = clientSentence.toUpperCase(); outToClient.printLine(responseSentence); } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection 3-Aug-18 Client-server Programming

25 Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; Scanner inFromUser = new Scanner(System.in); Socket clientSocket = new Socket("hostname", 6789); PrintWriter outToServer = new PrintWriter( new OutputStreamWriter( clientSocket.getOutputStream())); Create scanner Create client socket, connect to server Create output stream attached to socket 3-Aug-18 Client-server Programming

26 Example: Java client (TCP), cont.
Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.nextLine(); outToServer.println(sentence); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } Send line to server Read line from server 3-Aug-18 Client-server Programming


Download ppt "Client-server Programming"

Similar presentations


Ads by Google