130 likes | 303 Views
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);
E N D
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); • 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
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
Practice test question • What is the output on the computer running the Server? • What is the output on the computer running the Client? 4
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
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
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
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
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
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