From b483402ffbd8ba087bdd31c15d35c2b21981da29 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 13 Mar 2018 15:12:56 +0100 Subject: [PATCH] TD2: Fix the most important bugs in the server code. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD2/server/BroadcastThreadService.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/TD2/server/BroadcastThreadService.java b/TD2/server/BroadcastThreadService.java index 180f735..daf753f 100644 --- a/TD2/server/BroadcastThreadService.java +++ b/TD2/server/BroadcastThreadService.java @@ -18,9 +18,7 @@ public class BroadcastThreadService implements Runnable { public void run() { try { doService(clientSocket, listWriter); - //FIXME: also close the BR and PW? clientSocket.close(); - //FIXME: remove the associated PW from the ArrayList } catch (IOException e) { System.err.println("IOException : " + e); e.printStackTrace(); @@ -39,14 +37,22 @@ public class BroadcastThreadService implements Runnable { public void doService(Socket clientSocket, ArrayList sharedList) throws IOException { BufferedReader in; in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - sharedList.add(new PrintWriter(clientSocket.getOutputStream())); + PrintWriter OWriter = new PrintWriter(clientSocket.getOutputStream()); + sharedList.add(OWriter); boolean end = false; while (!end) { String theLine = in.readLine(); - if (theLine.equals(".")) + if (theLine.equals(".")) { end = true; // le thread de service doit terminer + break; // do not broadcast the dot that will close clients threads + } broadcastMsg(theLine); } + sharedList.remove(OWriter); + if (in != null) + in.close(); + if (OWriter != null) + OWriter.close(); System.out.println("Fin du thread repondant au client, port " + clientSocket.getPort()); } -- 2.34.1