1 / 10

Multi Threaded Chat Server

Multi Threaded Chat Server. Rick Mercer. 1. Client – Server with Socket Connections. We've seen how to establish a connection with 1 client Review a simple client /server connection next 2 slides ServerSocket serverSocket = new ServerSocket(4000);

tarala
Download Presentation

Multi Threaded Chat Server

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multi Threaded Chat Server • Rick Mercer 1

  2. Client – Server with Socket Connections • We've seen how to establish a connection with 1 client • Review a simple client /server connection next 2 slides • ServerSocket serverSocket = new ServerSocket(4000); • System.out.println("This server now awaits one client"); • Socket client = serverSocket.accept(); • ObjectOutputStream output = new • ObjectOutputStream(client.getOutputStream()); • ObjectInputStream input = new • ObjectInputStream(client.getInputStream()); • String clientInput = (String) input.readObject(); • System.out.println("Client wrote: " + clientInput); • output.writeObject("This server is shutting down."); • client.close(); 2

  3. Then run the client • Socket server = new Socket("localhost", 4000); • ObjectOutputStream output = new • ObjectOutputStream(server.getOutputStream()); • ObjectInputStream input = new • ObjectInputStream(server.getInputStream()); • output.writeObject("I am a client"); • String responseToMyOutput = (String) input.readObject(); • System.out.println("Server wrote: " + responseToMyOutput); • server.close(); 3

  4. Practice test question • What is the output on the computer running the Server? • What is the output on the computer running the Client? 4

  5. One Client at a time • Server could listen for many clients with an infinite loop • while(true) { /* do IO with each client */ • // Server code • ServerSocket serverSocket = new ServerSocket(4000); • while(true) { • Socket client = serverSocket.accept(); • ObjectOutputStream output = • new ObjectOutputStream(client.getOutputStream()); • output.writeObject("Please send money"); • } • But they all disappear after connecting as this program terminates • The server is still running 5

  6. Build a Chat Server, Client First • Need the client to check for server output without interrupting the GUI interaction • No "frozen" GUI please • Can type new messages and append incoming messages • At the same time • server writes this 6

  7. Review Threads • The JVM starts your main (and other threads) thread • We can start new stacks by calling run on a new Thread object • Using threads can make it appear we are doing things simultaneously • type into JTextField at the same time • Lines 75..84, ActionPerformed • read input from server Lines 93..97, the loop in IncomingReader run • However, when your Java program has the processor, the threads take turns using the processor 7

  8. Job: loop to read from server • The client needs something behind scenes to read read input from server • The code that reads input from the server has to run in a new Thread • Have to give the Thread--the worker--a runnable, which is the job the worker is supposed to do • Lines 52..53 8

  9. Chat Server • Main thread has a loop to accept new clients • Each time a new client is accepted, add its outputStream to an ArrayList • Also construct a ClientHandler that is all set up in a new thread • This thread will wait for subsequent input in a loop • The main thread is waiting for other connections • When any client writes to the server, the loop gets the message Line 51 and tells everyone • sdf 9

  10. Keeping track of many concurrent clients for some time • The server uses a separate thread for each client • Each thread can wait to read from that client • Code demo 10

More Related