TD2: Fix some comments.
[TD_SR.git] / TD2 / client / SocketClient.java
index 814d68a1ef3a62b74e72120a9d2e9b74bb7bb187..b038475de13bedc2abdd6109610cc9a7459d83d9 100644 (file)
@@ -18,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();
         }
     }
@@ -31,25 +46,34 @@ 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);
-        oLecture = new ObjectInputStream(IStream);
+        if (hasoStream)
+            oLecture = new ObjectInputStream(IStream);
+    }
 
-        OutputStream OStream = null;
-        OStream = sock.getOutputStream();
-        ecriture = new PrintWriter(OStream);
-        oEcriture = new ObjectOutputStream(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() {
@@ -71,7 +95,7 @@ public class SocketClient {
 
     /**
      * Send an object message on the opened client socket
-     * @param msg a string containing the message to send
+     * @param oMsg an object containing the message to send
      */
     public void sendoMsg(Message oMsg) throws IOException {
         oEcriture.writeObject(oMsg);
@@ -91,7 +115,7 @@ public class SocketClient {
 
     /**
      * Receive an object message sent on the opened client socket
-     * @return a string containing the received message
+     * @return a object containing the received message
      */
     public Message receiveoMsg() throws IOException, ClassNotFoundException {
         return (Message)oLecture.readObject();
@@ -116,6 +140,7 @@ public class SocketClient {
         }
         catch (IOException e) {
             System.err.println("IOException: " + e);
+            e.printStackTrace();
         }
     }