TD2: code cleanup in the client part.
[TD_SR.git] / TD2 / client / ClientSimplifie.java
diff --git a/TD2/client/ClientSimplifie.java b/TD2/client/ClientSimplifie.java
new file mode 100644 (file)
index 0000000..d247a93
--- /dev/null
@@ -0,0 +1,90 @@
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+public class ClientSimplifie {
+    BufferedReader lecture;  // pour le flot d'entrée venant du serveur
+    PrintWriter ecriture;  // pour le flot de sortie vers le serveur
+    Socket sock; // le socket client
+
+    public ClientSimplifie() {
+        // établie une connexion au serveur par un appel
+        // à connexionServeur()
+        connexionServeur("localhost", 5000);
+    }
+
+    public ClientSimplifie(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
+        try {
+            sock = new Socket(adresseIPServeur, portServeur);
+        }
+        catch (IOException e) {
+            System.err.println("IOException: " + e);
+        }
+        InputStream IStream = null;
+        try {
+            IStream = sock.getInputStream();
+        }
+        catch (IOException e) {
+            System.err.println("IOException: " + e);
+        }
+        InputStreamReader IMesg = new InputStreamReader(IStream);
+        lecture = new BufferedReader(IMesg);
+
+        OutputStream OStream = null;
+        try {
+            OStream = sock.getOutputStream();
+        }
+        catch (IOException e) {
+            System.err.println("IOException: " + e);
+        }
+        ecriture = new PrintWriter(OStream);
+    }
+
+    /**
+     * Send a message on the opened client socket
+     * @param msg a string containing the message to send
+     */
+    public void sendMsg(String msg) {
+        ecriture.println(msg);
+        ecriture.flush();
+    }
+
+    /**
+     * Receive a message sent on the opened client socket
+     * @return a string containing the received message
+     */
+    public String receiveMsg() {
+        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);
+        }
+        return line;
+    }
+
+    /**
+     * Close all opened I/O streams attached to this object instance
+     */
+    public void closeRWIO() {
+        ecriture.close();
+        try {
+            lecture.close();
+        }
+        catch (IOException e) {
+            System.err.println("IOException: " + e);
+        }
+    }
+
+} // fin classe ClientSimplifie