diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index db9059f..7b96316 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -16,27 +16,32 @@ public class ClientHandler implements Runnable { @Override public void run() { + try { - fromClientReader = new BufferedReader(new InputStreamReader(connectionToClient.getInputStream())); - toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); + fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); + toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); chatServer.broadcastMessage(name + " connected."); String message = fromClientReader.readLine(); while (message!=null) { chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); - } - } catch (IOException e) { - throw new RuntimeException(e); - - } finally { + } + } + + catch (IOException e) { + throw new RuntimeException(e); + } + + finally { //chatServer.removeClient(this); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { try { fromClientReader.close(); - } catch (IOException e){ + } + catch (IOException e){ e.printStackTrace(); } } @@ -50,5 +55,4 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); toClientWriter.flush(); } - }