X-Git-Url: https://git.piment-noir.org/?p=TD_SR.git;a=blobdiff_plain;f=TD2%2Fclient%2FThreadClientSend.java;h=59ffbd0bab538c80a6b6f82f3ab094089fa7f7d1;hp=69cd51a445a3879caa270103dd8a7056dd08c9f6;hb=e018d1ec033513412d9b3628a7d6701a48725382;hpb=58312685472236da9be0138c9b5b450ca13387b1 diff --git a/TD2/client/ThreadClientSend.java b/TD2/client/ThreadClientSend.java index 69cd51a..59ffbd0 100644 --- a/TD2/client/ThreadClientSend.java +++ b/TD2/client/ThreadClientSend.java @@ -1,36 +1,40 @@ import java.util.concurrent.ThreadLocalRandom; +import java.io.*; public class ThreadClientSend implements Runnable { private SocketClient client; - private String msg = new String(); ThreadClientSend(SocketClient c) { client = c; } - /** - * Message to sent setter - * @param m the text message to sent - */ - public void setMsg(String m) { - msg = m; - } - public void run() { - while (true) { - try { - client.sendMsg(msg); - System.out.println (Thread.currentThread().getName() + " a envoye " + msg); - try { - Thread.sleep(ThreadLocalRandom.current().nextInt(101)); - } - catch (InterruptedException e) { - System.err.println("InterruptedException: " + e); - } - } - catch (Exception e) { - System.err.println("Exception: " + e); + BufferedReader userInput = null; + try { + userInput = new BufferedReader(new InputStreamReader(System.in)); + boolean end = false; + while (!end) { + String line = userInput.readLine(); + if (line.equals(".")) { + end = true; + } + client.sendMsg(line); + System.out.println(Thread.currentThread().getName() + " a envoye " + line); } + } + catch (IOException e) { + System.err.println("IOException: " + e); + e.printStackTrace(); + } + finally { + if (userInput != null) { + try { + userInput.close(); + } + catch (IOException e) { + System.err.println("IOException: " + e); + } + } } } }