X-Git-Url: https://git.piment-noir.org/?p=TD_SR.git;a=blobdiff_plain;f=TD2%2Fclient%2FMain.java;h=81a31fb2e99b07607d82e5e10065039b5c2f2249;hp=cc47bb7bd499c54bf3822ccc7fbaea975795df27;hb=e018d1ec033513412d9b3628a7d6701a48725382;hpb=58312685472236da9be0138c9b5b450ca13387b1 diff --git a/TD2/client/Main.java b/TD2/client/Main.java index cc47bb7..81a31fb 100644 --- a/TD2/client/Main.java +++ b/TD2/client/Main.java @@ -7,46 +7,66 @@ public class Main { * Main for testing ClientSimplifie */ private static void main1() { - SocketClient client = new SocketClient(); + ClientSimplifie client = null; + BufferedReader userInput = null; try { - client.sendMsg("Line1"); - String msg = client.receiveMsg(); - System.out.println(msg); + client = new ClientSimplifie(); + userInput = new BufferedReader(new InputStreamReader(System.in)); + boolean end = false; + while (!end) { + String line = userInput.readLine(); + if (line.equals(".")) + end = true; + client.sendMsg(line); + String rline = client.receiveMsg(); + System.out.println(rline); + } } - catch (Exception e) { - System.err.println("Exception: " + e); + catch (IOException e) { + System.err.println("IOException: " + e); } finally { client.closeRWIO(); + if (userInput != null) { + try { + userInput.close(); + } + catch (IOException e) { + System.err.println("IOException: " + e); + } + } } } - public static void main (String[] args) { - SocketClient client = new SocketClient(); + SocketClient client = null; + Thread thS = null; + Thread thR = null; - // try { - ThreadClientSend thCS = new ThreadClientSend(client); - //FIXME: Implement a loop based on user input to set dynamically the message to sent. - //for (int i = 0; i < 10; i++) { - // thCS.setMsg("Line" + i); - //} - thCS.setMsg("Line1"); - Thread thS = new Thread(thCS); - Thread thR = new Thread(new ThreadClientReceive(client)); + try { + client = new SocketClient(); + thS = new Thread(new ThreadClientSend(client)); + thR = new Thread(new ThreadClientReceive(client)); thS.setName("thS"); thS.start(); thR.setName("thR"); thR.start(); - // } - // catch (Exception e) { - // System.err.println("Exception: " + e); - // } - // finally { - // client.closeRWIO(); - // } - + } + catch (Exception e) { + System.err.println("Exception: " + e); + } + finally { + try { + thS.join(); + thR.join(); + } + catch (InterruptedException e) { + System.err.println("InterruptedException: " + e); + e.printStackTrace(); + } + client.closeRWIO(); + } } }