X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD2%2Fclient%2FSocketClient.java;h=3c6d021c605f3a22a7d9537d8f1bf830a28854f7;hb=da31e6daf7553c6b38ca83c428dd02a3aa8af853;hp=48a93eb75964660931a2f0cb16f18f082e51167d;hpb=58312685472236da9be0138c9b5b450ca13387b1;p=TD_SR.git diff --git a/TD2/client/SocketClient.java b/TD2/client/SocketClient.java index 48a93eb..3c6d021 100644 --- a/TD2/client/SocketClient.java +++ b/TD2/client/SocketClient.java @@ -5,92 +5,113 @@ 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() { // établie une connexion au serveur par un appel // à connexionServeur() - connexionServeur("localhost", 5000); - } - - public SocketClient(String adresseIPServeur, int portServeur) { - // établie une connexion au serveur par un appel - // à connexionServeur() - connexionServeur(adresseIPServeur, portServeur); - } - - private void connexionServeur(String adresseIPServeur, int portServeur) { - // créer un objet socket lié au socket serveur et l'affecte à sock - // puis établie les chaînages de flot nécessaires - // pour l'envoi et la reception de messages + attributesInit(); try { - sock = new Socket(adresseIPServeur, portServeur); + connexionServeur("localhost", 5000); } catch (IOException e) { System.err.println("IOException: " + e); + closeRWIO(); } - InputStream IStream = null; + } + + public SocketClient(String adresseIPServeur, int portServeur) { + // établie une connexion au serveur par un appel + // à connexionServeur() + attributesInit(); try { - IStream = sock.getInputStream(); + connexionServeur(adresseIPServeur, portServeur); } catch (IOException e) { System.err.println("IOException: " + e); + closeRWIO(); } + } + + private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException { + // créer un objet socket lié au socket serveur et l'affecte à sock + // puis établie les chaînages de flot nécessaires + // pour l'envoi et la reception de messages + sock = new Socket(adresseIPServeur, portServeur); + + OutputStream OStream = sock.getOutputStream(); + ecriture = new PrintWriter(OStream); + oEcriture = new ObjectOutputStream(OStream); + + InputStream IStream = sock.getInputStream(); InputStreamReader IMesg = new InputStreamReader(IStream); lecture = new BufferedReader(IMesg); + oLecture = new ObjectInputStream(IStream); + } - OutputStream OStream = null; - try { - OStream = sock.getOutputStream(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } - ecriture = new PrintWriter(OStream); + private void attributesInit() { + sock = null; + lecture = null; + ecriture = null; + oLecture = null; + oEcriture = null; } /** * Send a message on the opened client socket * @param msg a string containing the message to send */ - public synchronized void sendMsg(String msg) { - //NOTE: it's not really required with one socket writer thread. - while (msg.isEmpty()) { - try { - wait(); - } - catch (InterruptedException e) { - System.err.println("InterruptedException: " + e); - } - } + public void sendMsg(String msg) { ecriture.println(msg); ecriture.flush(); - notifyAll(); + } + + /** + * 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() { + public String receiveMsg() throws IOException { String line = new String(); - try { - //FIXME: read only the line before the ending newline - line = lecture.readLine(); - } - catch (IOException e) { - System.err.println("IOException: " + e); - } + //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 */ public void closeRWIO() { - ecriture.close(); try { - lecture.close(); + if (sock != null) + sock.close(); + if (lecture != null) + 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);