import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Vector; import java.io.*; public class ChatServer extends Thread implements IMessageHandler { int iPort; Vector vClientInfo; public ChatServer (int iPort) { this.iPort = iPort; this.vClientInfo = new Vector(); this.start(); } public void run () { // Create new socket ServerSocket listener = null; try { listener = new ServerSocket(iPort); } catch (Exception e) { System.out.println("Error creating socket listener. " + e.toString()); System.exit(1); } // listen for connections while (true) { try { System.out.println("Chat Server waiting for connections..."); Socket newChatClient = listener.accept(); // process new connection handleNewClient (newChatClient); } catch (IOException ioe) { System.out.println("Error handling new connection. " + ioe.toString()); System.exit(1); } } } public void handleNewClient (Socket newClientSocket) { try { OutputStream dos = newClientSocket.getOutputStream(); InputStream dis = newClientSocket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(dis)); PrintWriter pw = new PrintWriter(dos, true); ClientInfo newClient = new ClientInfo(); System.out.println("Accepted connection from " + br.readLine()); // set Client Info parameters // add client and message all users that someone new has // connected... you need to code this up... } catch (IOException ioe) { System.out.println("Error handling new client. " + ioe.toString()); } } public synchronized void handleMessage (String sSpeakerName, String sMessage) { if (sSpeakerName.equals("DISCONNECT")) { // terminate client removeClient(sMessage); } else { // send to all clients // you need to code this up... } } private void removeClient(String sUserName) { int iNumClients = vClientInfo.size(); // you need to code this up } public static void main (String args[]) { if (args.length != 1) { System.out.println("Usage: java ChatServer "); System.exit(1); } try { int iPort = Integer.parseInt(args[0]); new ChatServer(iPort); } catch (NumberFormatException ife) { System.out.println("Port must be an integer. " + ife.toString()); System.exit(1); } } } class ClientInfo { String sUserName; Socket sockClient; PrintWriter speakerChannel; ClientProxy clientProxy; }