// Server program for Gnat Chat to receive a Message object from client // and send to other client. // Has two threads for each side of the communication. // October 30, 2008 CS479 Dan Hyde import java.io.*; import java.net.*; public class ChatServer { public ChatServer() { ServerSocket server = null; Socket connect1 = null; Socket connect2 = null; ObjectOutputStream out1 = null; ObjectOutputStream out2 = null; ObjectInputStream in1 = null; ObjectInputStream in2 = null; Thread thread1 = null; Thread thread2 = null; // port 5000 try { server = new ServerSocket(5000, 100); } catch (IOException e) { System.err.println("Error in socket " + e); } while (true) { try { System.out.println("\n--- Welcome to Gnat Chat Server ---" + "\n\nWaiting for first client ...."); connect1 = server.accept(); System.out.println("Connection received from " + connect1.getInetAddress().getHostName()); try{ out1 = new ObjectOutputStream(connect1.getOutputStream()); in1 = new ObjectInputStream(connect1.getInputStream()); } catch ( IOException e5 ){ System.err.println("Error in connection " + e5); System.exit(0); } System.out.println("Waiting for second client ...."); connect2 = server.accept(); System.out.println("Connection received from " + connect2.getInetAddress().getHostName()); try{ out2 = new ObjectOutputStream(connect2.getOutputStream()); in2 = new ObjectInputStream( connect2.getInputStream()); } catch (IOException e5){ System.err.println("Error in connection " + e5); System.exit(0); } // start a new thread thread1 = new OneSideThread(in1, out2, connect1); // start a new second thread thread2 = new OneSideThread(in2, out1, connect2); } catch (IOException e5){ System.err.println("Error in connection " + e5.getMessage()); } // wait for shut down try { thread1.join(); thread2.join(); } catch (InterruptedException e3) { System.err.println(e3.getMessage()); } // close streams and sockets try { in1.close(); out1.close(); connect1.close(); System.out.println("Connection closed from " + connect1.getInetAddress().getHostName()); in2.close(); out2.close(); connect2.close(); System.out.println("Connection closed from " + connect2.getInetAddress().getHostName()); } catch (IOException e5) { System.err.println("Error in connection " + e5); System.exit(0); } } } public static void main (String args []) { ChatServer app = new ChatServer(); } } // Thread that handles one side of the communication through the server. class OneSideThread extends Thread { private ObjectInputStream in = null;; private ObjectOutputStream out = null; private Socket theSocket = null; public OneSideThread(ObjectInputStream inP, ObjectOutputStream outP, Socket inSocket) { in = inP; out = outP; theSocket = inSocket; this.start(); // call run() } public void run() { Message mess = new Message(); boolean shutDown = false; do { try { mess = (Message) in.readObject(); shutDown = mess.getShutDown(); out.writeObject(mess); } catch (ClassNotFoundException e3) { System.err.println("Class not found " + e3); System.exit(0); } catch (IOException e4){ System.err.println("Error in I/O " + e4); System.exit(0); } } while(!shutDown); } }