/** Allow this dedicated thread to handle input from this client */ public void run() { try { ClientMessage myMessage = (ClientMessage) clObj.getObIn().readObject(); this.username = myMessage.getSender(); clObj.setName(username); System.out.println("Server gets: " + myMessage.toString()); clientList.userConnect(username, clObj); } catch (Exception e) { JOptionPane.showMessageDialog(panel, "Unrecoverablable error in input stream"); e.printStackTrace(); System.exit(1); } while (true) { try { ClientMessage myMessage = (ClientMessage) clObj.getObIn().readObject(); System.out.println("Server gets: " + myMessage.toString()); List<ObjectOutputStream> destinations = clientList.getUserOutStreams(myMessage.getDestinations()); for (ObjectOutputStream s : destinations) { s.writeObject(new ServerMessage(this.username, myMessage.getMessage())); } } catch (SocketException e) { System.out.println("Disconnecting user: "******"Some other error in client handler (run)"); JOptionPane.showMessageDialog(panel, "Unrecoverablable error in input stream"); ex.printStackTrace(); System.exit(1); break; } } }
/** Runs the thread to accept new connections */ @Override public void run() { try { while (true) { Socket S; S = serverSocket.accept(); ClientHandler ch = new ClientHandler(S); ch.start(); } } catch (Exception e) { JOptionPane.showMessageDialog(panel, "Unrecoverablable error in input stream"); e.printStackTrace(); System.exit(1); } }
/** * Establishes the threads to handle the clients * * @param pn Sever panel to send output to */ public Server(ServerPanel pn) { panel = pn; clientList = new ClientList(); try { serverSocket = new ServerSocket(0); } catch (Exception e) { JOptionPane.showMessageDialog(panel, "Unrecoverablable establishing a server socket"); e.printStackTrace(); System.exit(1); } try { String hostName = InetAddress.getLocalHost().getHostAddress(); int port = serverSocket.getLocalPort(); panel.setInfo(hostName, port); } catch (UnknownHostException e) { JOptionPane.showMessageDialog(panel, "Unable to connet client to self"); e.printStackTrace(); System.exit(1); } this.start(); }