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);
+ }
+ }
}
}
}