X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD2%2Fclient%2FSocketClient.java;h=814d68a1ef3a62b74e72120a9d2e9b74bb7bb187;hb=c4aaaecaa30716d0bc52f859f799880e5a37b027;hp=b8e0c0b35491a30ae89bde0908b34ecbfb680697;hpb=e018d1ec033513412d9b3628a7d6701a48725382;p=TD_SR.git diff --git a/TD2/client/SocketClient.java b/TD2/client/SocketClient.java index b8e0c0b..814d68a 100644 --- a/TD2/client/SocketClient.java +++ b/TD2/client/SocketClient.java @@ -5,6 +5,8 @@ import java.util.*; public class SocketClient { BufferedReader lecture; // pour le flot d'entrée venant du serveur PrintWriter ecriture; // pour le flot de sortie vers le serveur + ObjectInput oLecture; + ObjectOutput oEcriture; Socket sock; // le socket client public SocketClient() { @@ -42,16 +44,20 @@ public class SocketClient { IStream = sock.getInputStream(); InputStreamReader IMesg = new InputStreamReader(IStream); lecture = new BufferedReader(IMesg); + oLecture = new ObjectInputStream(IStream); OutputStream OStream = null; OStream = sock.getOutputStream(); ecriture = new PrintWriter(OStream); + oEcriture = new ObjectOutputStream(OStream); } private void attributesInit() { + sock = null; lecture = null; ecriture = null; - sock = null; + oLecture = null; + oEcriture = null; } /** @@ -63,17 +69,34 @@ public class SocketClient { ecriture.flush(); } + /** + * Send an object message on the opened client socket + * @param msg a string containing the message to send + */ + public void sendoMsg(Message oMsg) throws IOException { + oEcriture.writeObject(oMsg); + oEcriture.flush(); + } + /** * Receive a message sent on the opened client socket * @return a string containing the received message */ public String receiveMsg() throws IOException { String line = new String(); - //FIXME: read only the line before the ending newline + //FIXME?: read only the line before the ending newline line = lecture.readLine(); return line; } + /** + * Receive an object message sent on the opened client socket + * @return a string containing the received message + */ + public Message receiveoMsg() throws IOException, ClassNotFoundException { + return (Message)oLecture.readObject(); + } + /** * Close all opened I/O streams attached to this object instance */ @@ -85,6 +108,11 @@ public class SocketClient { lecture.close(); if (ecriture != null) ecriture.close(); + if (oLecture != null) + oLecture.close(); + if (oEcriture != null) { + oEcriture.close(); + } } catch (IOException e) { System.err.println("IOException: " + e);