| 1 | import java.net.*; |
| 2 | import java.io.*; |
| 3 | import java.util.*; |
| 4 | |
| 5 | public class EchoServerThreadService implements Runnable { |
| 6 | |
| 7 | private Socket clientSocket; |
| 8 | private ArrayList<PrintWriter> listWriter; |
| 9 | |
| 10 | EchoServerThreadService(Socket clientSocket) { |
| 11 | System.out.println("Creation d'un thread pour repondre a un client, port " + clientSocket.getPort()); |
| 12 | this.clientSocket = clientSocket; |
| 13 | } |
| 14 | |
| 15 | public void run() { |
| 16 | try { |
| 17 | doService(clientSocket); |
| 18 | clientSocket.close(); |
| 19 | } catch (IOException e) { |
| 20 | System.err.println("IOException : " + e); |
| 21 | e.printStackTrace(); |
| 22 | } |
| 23 | finally { |
| 24 | try { |
| 25 | if (this.clientSocket != null) |
| 26 | this.clientSocket.close(); |
| 27 | } catch (IOException e) { |
| 28 | System.err.println("IOException : " + e); |
| 29 | e.printStackTrace(); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public void doService(Socket clientSocket) throws IOException { |
| 35 | BufferedReader in; |
| 36 | PrintStream out; |
| 37 | in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); |
| 38 | out = new PrintStream(clientSocket.getOutputStream()); |
| 39 | //listWriter.add(new PrintWriter(clientSocket.getOutputStream())); |
| 40 | boolean end = false; |
| 41 | while (!end) { |
| 42 | String theLine = in.readLine(); |
| 43 | if (theLine.equals(".")) |
| 44 | end = true; // le thread de service doit terminer |
| 45 | out.println(theLine); |
| 46 | } |
| 47 | System.out.println("Fin du thread repondant au client, port " |
| 48 | + clientSocket.getPort()); |
| 49 | } |
| 50 | |
| 51 | public void broadcastMsg(String msg) { |
| 52 | for (int i = 0; i < listWriter.size(); i++) { |
| 53 | listWriter.get(i); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | } |