Site hosted by Angelfire.com: Build your free website today!
 

The Chat Application

The structure of the applications:

  • Server side

    • Data members

        HashMap clientSockets = new HashMap();
        ServerSocket serverSocket;
    • The Constructor

        public ChatServer(int port) throws IOException {
          serverSocket = new ServerSocket(port);
        }
    • While-Accept loop

       protected void clientConnectListener() throws IOException {
          while (true) {
            Socket newSocket = serverSocket.accept();
            clientSockets.put(newSocket, 
              new DataOutputStream(newSocket.getOutputStream()));
            new ServerThread(this, newSocket);  // an independ thread for each client
          }
       }
    • The sendToAll(String message) method

        synchronized protected void sendToAll(String message) {
          Iterator it = clientSockets.values().iterator();
          while (it.hasNext()) {
            DataOutputStream dout = (DataOutputStream)it.next();
            try {
              dout.writeUTF(message);
            } catch (IOException e) {
              System.out.println("Error writing to client: " + e);
            }
          }
        }
    • The removeConnection(Socket socket) method.

       synchronized protected void removeConnection(Socket socket) {
         clientSockets.remove(socket);
       }
    • The main() method

        int port = Integer.parseInt(args[0]);
        ...
        new ChatServer(port).clientConnectListener();
  • Per-Client Thread

    • The run() method should implements a while loop that read message from the client and passes the message to the server.

       protected ServerThread(ChatServer server, Socket socket) {
         this.server = server;
         this.socket = socket;
         start();
       } 
       public void run() {
         while(true) {
           ...  // read message from client
           server.sendToAll(message);
           ...  // if EOFException occurs inform the server to remove
                // this client socket from the list of client of socket,
                // and terminate the thread/method
         }
       }
  • Swing Client implements runnable

    • The Constructor

        public ChatClient(String host, int port) throws IOException {
          ... // Establish connection to the server
          ... // Build the GUI
          new Thread(this).start(); // start the server message loop
        }
    • The user interface - consists of a JTextArea for displaying messages sent from the ChatServer, and a JTextFiled for user to input their messages.

    • User Input handling - an ActionListener to handler ActionPerformed Event of a JTextField and send the content to the ChatServer

    • A thread to read the message from the ChatServer and append to a JTextArea

        public void run() {
           try {
             while (true) {
               ... // read message from ChatServer
               textArea.append(message + "\n");
             }
           } catch (...) {
             ...
           }
        }
    • The main() method

        public static void main(String args[]) {
          ChatClient client = null;
          try {
            client = new ChatClient(args[0], Integer.parseInt(args[1]));
          } catch (IOException e) {
            System.out.println("Unable to establish connection to server" + e);
            System.exit(0);
          }
          client.pack();
          client.setVisible(true);
        }