83baf074a6fa48f3cf5d0ccb5cf4983b.ppt
- Количество слайдов: 10
Multi Threaded Chat Server Rick Mercer
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 Server. Socket server. Socket = new Server. Socket(4000); System. out. println("This server now awaits one client"); Socket client = server. Socket. accept(); Object. Output. Stream output = new Object. Output. Stream(client. get. Output. Stream()); Object. Input. Stream input = new Object. Input. Stream(client. get. Input. Stream()); String client. Input = (String) input. read. Object(); System. out. println("Client wrote: " + client. Input); output. write. Object("This server is shutting down. "); client. close();
Then run the client Socket server = new Socket("localhost", 4000); Object. Output. Stream output = new Object. Output. Stream(server. get. Output. Stream()); Object. Input. Stream input = new Object. Input. Stream(server. get. Input. Stream()); output. write. Object("I am a client"); String response. To. My. Output = (String) input. read. Object(); System. out. println("Server wrote: " + response. To. My. Output); server. close();
Practice test question What is the output on the computer running the Server? What is the output on the computer running the Client?
One Client at a time Server could listen for many clients with an infinite loop while(true) { /* do IO with each client */ // Server code Server. Socket server. Socket = new Server. Socket(4000); while (true) { Socket client = server. Socket. accept(); Object. Output. Stream output = new Object. Output. Stream(client. get. Output. Stream()); output. write. Object("Please send money"); } But they all disappear after connecting as this program terminates The server is still running
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 JText. Field at the same time Lines 75. . 84, Action. Performed read input from server Lines 93. . 97, the loop in Incoming. Reader run However, when your Java program has the 7 processor, the threads take turns using the processor
Job: loop to read from server The client needs something behind scenes to 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 output. Stream to an Array. List Also construct a Client. Handler 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