TCP Programming Exercise - Chat Server |
This exercise is designed to illustrate and provide practice for network programming tasks common to many server applications. The format of the exercise is:
Chat Server - Chat servers connect two or more clients so that the input of one client is output to all other clients. Attempt number zero of a chat server that reads a line of text from the keyboard and prints to the screen is below. No networking is done so the server isn't really a server but does illustrate some of the general structure of a chat server.
// chatserver0.java Use: java -cp . chatserver0 import java.io.*; class chatserver0 { public static void main(String args[]) throws Exception {new server0().run(); // run server }} class server0 { public void run() { String from;BufferedReader in=null; // input and output PrintStream out=null; try { in = new BufferedReader(new InputStreamReader( System.in )); out = new PrintStream( System.out );System.out.println("Connected"); // Read line while( (from=in.readLine()) != null && !from.equals("")) { System.out.println( from );out.print(from + "\r\n"); // Print line } }catch(IOException e) {} // catch IO errors System.out.println("Disconnected"); }} |
Java Programming Points
Hello World\r\n from="Hello World"
|
Exercise 0 - Obviously chat version 0 needs networking. Try to identify points within the program where:
Exercise 1 - Chat server version 1 implements network communication with a TCP client. Test the chat server by:
1. whether more than one client telnet sessions can be handled by the chat server at the same time,
2. whether it continues after the client connection is closed,
3. other server weaknesses.
// chatserver1.java Use: java -cp . chatserver1 import java.net.*; import java.io.*; class chatserver1 { public static void main(String args[]) throws Exception {ServerSocket conn = new ServerSocket( 1025 ); //find a working port new server1( conn.accept() ).run(); // Wait for connection/run server }} class server1 {Socket s; server1(Socket s) { // Construct new server1this.s = s; // Socket accepted a connection } public void run() { String from;BufferedReader in=null; // Socket input and output PrintStream out=null; try { in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintStream(s.getOutputStream());System.out.println("Connected"); // Read line from client while( (from=in.readLine()) != null && !from.equals("")) { System.out.println( from );out.print(from + "\r\n"); // Print line to client }s.close(); // Close connection }catch(IOException e) {} // catch IO errors System.out.println("Disconnected"); }} |
Comparison of Non-networked and
Networked Program
// chatserver0.java Use: java -cp . chatserver0 import java.io.*; class chatserver0 { public static void main(String args[]) throws Exception {new server0().run(); }} class server0 { public void run() { String from; BufferedReader in=null; PrintStream out=null; try { in = new BufferedReader( new InputStreamReader(System.in)); out = new PrintStream(System.out);System.out.println("Connected"); while( (from=in.readLine()) != null && !from.equals("")) { System.out.println( from );out.print(from + "\r\n"); } }catch(IOException e) {} System.out.println("Disconnected"); }} |
// chatserver1.java Use: java -cp . chatserver1 import java.net.*; import java.io.*; class chatserver1 { public static void main(String args[]) throws Exception {ServerSocket conn = new ServerSocket( 1025 ); new server1(conn.accept()).run(); }} class server1 {Socket s; server1(Socket s) { this.s = s; } public void run() { String from; BufferedReader in=null; PrintStream out=null; try { in = new BufferedReader( new InputStreamReader(s.getInputStream())); out = new PrintStream(s.getOutputStream());System.out.println("Connected"); while( (from=in.readLine()) != null && !from.equals("")) { System.out.println( from );out.print(from + "\r\n"); } s.close(); } catch(IOException e) {} System.out.println("Disconnected"); }} |
Java Programming Points
Hello World\r\n from="Hello World"
|
Exercise 2 - One weakness is that the server terminates when the client connection is closed. The server should wait for another connection rather than terminating.
Exercise 3 - A serious weakness for the chat server is that only a single client can be connected at a time. The server should allow multiple simultaneous connections.
Exercise 4 - The server still has a serious weakness in that the multiple clients can't chat to each other!
Exercise 5 - The server should usually behave as expected but has a potential hazard. Each client connection is running in a separate thread on the server. When multiple clients attempt to connect at the same time, updates to the vector holding the client connection is in a race condition. One thread could begin accessing the vector, be suspended, and another thread undo or corrupt the vector.