X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD2%2Fclient%2FSocketClient.java;h=15ed27935892cd325019bb81689a6ce69037323a;hb=1a8b0826b5021061a361550b8a83cde7c25955cb;hp=8bc5d4f7f81567253779c019e330908dca950a49;hpb=ef258436b31ba460b5038c5b7b62a06ea8968b2d;p=TD_SR.git diff --git a/TD2/client/SocketClient.java b/TD2/client/SocketClient.java index 8bc5d4f..15ed279 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() { @@ -16,6 +18,21 @@ public class SocketClient { } catch (IOException e) { System.err.println("IOException: " + e); + e.printStackTrace(); + closeRWIO(); + } + } + + public SocketClient(boolean withoStream) { + // établie une connexion au serveur par un appel + // à connexionServeur() + attributesInit(); + try { + connexionServeur("localhost", 5000, withoStream); + } + catch (IOException e) { + System.err.println("IOException: " + e); + e.printStackTrace(); closeRWIO(); } } @@ -29,29 +46,42 @@ public class SocketClient { } catch (IOException e) { System.err.println("IOException: " + e); + e.printStackTrace(); closeRWIO(); } } - private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException { + private void connexionServeur(String adresseIPServeur, int portServeur, boolean hasoStream) 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); - InputStream IStream = null; - IStream = sock.getInputStream(); + + OutputStream OStream = sock.getOutputStream(); + ecriture = new PrintWriter(OStream); + if (hasoStream) + oEcriture = new ObjectOutputStream(OStream); + + InputStream IStream = sock.getInputStream(); InputStreamReader IMesg = new InputStreamReader(IStream); lecture = new BufferedReader(IMesg); + if (hasoStream) + oLecture = new ObjectInputStream(IStream); + } - OutputStream OStream = null; - OStream = sock.getOutputStream(); - ecriture = new PrintWriter(OStream); + 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 + connexionServeur(adresseIPServeur, portServeur, false); } private void attributesInit() { + sock = null; lecture = null; ecriture = null; - sock = null; + oLecture = null; + oEcriture = null; } /** @@ -63,6 +93,15 @@ 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 @@ -74,6 +113,14 @@ public class SocketClient { 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,9 +132,15 @@ 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); + e.printStackTrace(); } }