1 / 36

Client/Server Distributed Systems

Client/Server Distributed Systems. 240-322, Semester 1, 2005-2006. Objectives discuss a client/server based chat system mention two other ways of chatting. 12. Java Chat. Contents. 1. Chat Characteristics 2. Threaded TCP Client/Server Chat 3. Other Approaches. 1. Chat Characteristics.

tia
Download Presentation

Client/Server Distributed Systems

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. Client/Server Distributed Systems 240-322, Semester 1, 2005-2006 • Objectives • discuss a client/server based chat system • mention two other ways of chatting 12. Java Chat

  2. Contents 1. Chat Characteristics 2. Threaded TCP Client/Server Chat 3. Other Approaches

  3. 1. Chat Characteristics • Client/Server or P2P server messages clients clients continued

  4. Any number of clients. • Clients can join/depart at any time. • There's no ordering to the communication • anyone can talk at any time • a message can arrive at a client at any time • a message can be sent by a client at any time

  5. 1.1 Chatting Compared to Games • Typical games are chess, monopoly, tic-tac-toe. These types of games have: • a fixed number of players • the game starts only when all the players are present • the players take fixed turns continued

  6. But chatting is similar to multiplayer online games (MMOGs) such as City Of Heroes, Ragnarok. • The chat communication model is used by MMOGs.

  7. 1.2. Advantages of a Server Model • Client control • e.g. a server can filter client messages • Management • e.g. login/logout, message routing, billing • Resource Storage • e.g. message logs, images, movies, sounds • These features are harder to support in P2P.

  8. 1.3. Disadvantages of a Server Model • A single point of failure. • A communications bottleneck. • Increased message latency. • Many of these problems are solved by using P2P, or by using multiple servers.

  9. 1.4. More Information • The examples in these slides come from: Killer Game Programming in Java (KGPJ)Chapter 30http://fivedots.coe.psu.ac.th/~ad/jg/ch19 • A draft of the chapter, and all the code, can be found there.

  10. 2. Threaded TCP Client/Server Chat continued

  11. 2.1. Client Classes • ChatClient • maintains the GUI, processes the user's input, and sends messages to the server over a TCP link • ChatWatcher • waits for messages from the server, and displays them in ChatClient's GUI text area

  12. 2.2. Client-side Messages • ChatClient can send the following messages: • who • bye • any text message messages server client

  13. 2.3. Server Classes • ChatServer • the top-level server, which spawns a ChatServerHandler thread when a new client connects to it • ChatServerHandler • handles the communication with a client • ChatGroup • holds client details (e.g. input streams), accessible via synchronized methods

  14. 2.4. Server-side Messages • WHO$$ cliAddr1 & port1 & ... cliAddrN & portN & • sent back to a client in response to a "who" message • (cliAddr, port): message • broadcast to all the clients messages server clients

  15. 2.5. ChatClient sends the bye message shows messages received from the server sends a text message sends the who message

  16. Contacting the Server // globals// server detailsprivate static final int PORT = 1234; private static final String HOST = "localhost";private Socket sock;private PrintWriter out; // output to the server continued

  17. private void makeContact(){ try { sock = new Socket(HOST, PORT); BufferedReader in = new BufferedReader( new InputStreamReader( sock.getInputStream()) ); out = new PrintWriter(sock.getOutputStream(),true ); new ChatWatcher(this, in).start(); // watch for server msgs } catch(Exception e) { System.out.println(e); }}

  18. 2.6. ChatWatcher Thread from server run() { // wait for message // process it // show in GUI} WHO$$, broadcast msg looping ChatWatcher (on client-side) continued

  19. while ((line = in.readLine()) != null) { if ((line.length() >= 6) && // "WHO$$ " (line.substring(0,5).equals("WHO$$"))) showWho( line.substring(5).trim() ); // remove WHO$$ keyword and spaces else // show immediatelyclient.showMsg(line + "\n");}

  20. 2.7. ChatServer port1234 client connections // wait for client connection // create handler for client looping ChatServer (on the server-side) continued

  21. public ChatServer(){ cg = new ChatGroup(); try { ServerSocket serverSock = new ServerSocket(PORT); Socket clientSock; while (true) { System.out.println("Waiting for a client..."); clientSock = serverSock.accept(); new ChatServerHandler(clientSock, cg).start(); } } catch(Exception e) { System.out.println(e); }}

  22. 2.8. ChatServerHandler • A handler is created for each client that contacts the server. • The handler is a thread that processes all the clients interaction with the server. continued

  23. public void run(){ try { // Get I/O streams from the socket BufferedReader in = new BufferedReader( new InputStreamReader( clientSock.getInputStream())); PrintWriter out = new PrintWriter( clientSock.getOutputStream(), true); cg.addPerson(cliAddr, port, out); // add client to ChatGroupprocessClient(in, out); // interact with client : continued

  24. // the client has finished when execution // reaches here cg.delPerson(cliAddr, port); // remove client details clientSock.close(); System.out.println("Client (" + cliAddr + ", " + port + ") connection closed\n"); } catch(Exception e) { System.out.println(e); }}

  25. processClient() • A loop which: • waits for a message from the client • processes the message, using doRequest() • if a response (or broadcast) is needed, it is carried out by the ChatGroup object continued

  26. private void doRequest(String line, PrintWriter out){ if (line.trim().toLowerCase().equals("who")) { System.out.println("Processing 'who'"); out.println( cg.who() ); } else // use ChatGroup object to broadcast messagecg.broadcast( "("+cliAddr+", "+port+"): " + line);} cg is the ChatGroup object

  27. 2.9. ChatGroup • Stores client details in Chatter objects • 1 Chatter object per client • details include client input stream • Answers "who" messages. • Handles broadcasting. • ChatGroup uses synchronized methods to control access to the clients streams. continued

  28. Why is ChatGroup Synchronized?

  29. A ChatGroup Method synchronized public void broadcast(String msg){ Chatter c; for(int i=0; i < chatPeople.size(); i++) { c = (Chatter) chatPeople.get(i); c.sendMessage(msg); }} // end of broadcast()

  30. 2.10. The Chatter Class • A Chatter object manages a client's address, port and a PrintWriter stream going to the client. private PrintWriter out; // globalpublic void sendMessage(String msg){ out.println(msg); }

  31. 3. Other Approaches • Chapter 30 in KGPJ contains two other chat implementations, using: • UDP multicasting (a form of P2P) • a servlet server

  32. 3.1. UDP Multicasting Clients and a Name Server continued

  33. Not pure P2P -- it uses a login name server. • NameServer deals with who messages • reduces the packets volume in the multicast group • Main problem: there's no easy way to control access to the multicast group.

  34. 3.2. Servlet as a Server continued

  35. The key difference between this approach and the standard client/server is that ChatServlet cannot start communication with a client. • URLChatWatcher must periodically poll the ChatServlet for messages • Since the servlet is contacted using HTTP, it will work through most firewalls.

More Related