| 1 | import java.io.*; |
| 2 | import java.net.*; |
| 3 | import java.util.*; |
| 4 | |
| 5 | public class SocketClient { |
| 6 | BufferedReader lecture; // pour le flot d'entrée venant du serveur |
| 7 | PrintWriter ecriture; // pour le flot de sortie vers le serveur |
| 8 | Socket sock; // le socket client |
| 9 | |
| 10 | public SocketClient() { |
| 11 | // établie une connexion au serveur par un appel |
| 12 | // à connexionServeur() |
| 13 | connexionServeur("localhost", 5000); |
| 14 | } |
| 15 | |
| 16 | public SocketClient(String adresseIPServeur, int portServeur) { |
| 17 | // établie une connexion au serveur par un appel |
| 18 | // à connexionServeur() |
| 19 | connexionServeur(adresseIPServeur, portServeur); |
| 20 | } |
| 21 | |
| 22 | private void connexionServeur(String adresseIPServeur, int portServeur) { |
| 23 | // créer un objet socket lié au socket serveur et l'affecte à sock |
| 24 | // puis établie les chaînages de flot nécessaires |
| 25 | // pour l'envoi et la reception de messages |
| 26 | try { |
| 27 | sock = new Socket(adresseIPServeur, portServeur); |
| 28 | } |
| 29 | catch (IOException e) { |
| 30 | System.err.println("IOException: " + e); |
| 31 | } |
| 32 | InputStream IStream = null; |
| 33 | try { |
| 34 | IStream = sock.getInputStream(); |
| 35 | } |
| 36 | catch (IOException e) { |
| 37 | System.err.println("IOException: " + e); |
| 38 | } |
| 39 | InputStreamReader IMesg = new InputStreamReader(IStream); |
| 40 | lecture = new BufferedReader(IMesg); |
| 41 | |
| 42 | OutputStream OStream = null; |
| 43 | try { |
| 44 | OStream = sock.getOutputStream(); |
| 45 | } |
| 46 | catch (IOException e) { |
| 47 | System.err.println("IOException: " + e); |
| 48 | } |
| 49 | ecriture = new PrintWriter(OStream); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Send a message on the opened client socket |
| 54 | * @param msg a string containing the message to send |
| 55 | */ |
| 56 | public synchronized void sendMsg(String msg) { |
| 57 | //NOTE: it's not really required with one socket writer thread. |
| 58 | while (msg.isEmpty()) { |
| 59 | try { |
| 60 | wait(); |
| 61 | } |
| 62 | catch (InterruptedException e) { |
| 63 | System.err.println("InterruptedException: " + e); |
| 64 | } |
| 65 | } |
| 66 | ecriture.println(msg); |
| 67 | ecriture.flush(); |
| 68 | notifyAll(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Receive a message sent on the opened client socket |
| 73 | * @return a string containing the received message |
| 74 | */ |
| 75 | public String receiveMsg() { |
| 76 | String line = new String(); |
| 77 | try { |
| 78 | //FIXME: read only the line before the ending newline |
| 79 | line = lecture.readLine(); |
| 80 | } |
| 81 | catch (IOException e) { |
| 82 | System.err.println("IOException: " + e); |
| 83 | } |
| 84 | return line; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Close all opened I/O streams attached to this object instance |
| 89 | */ |
| 90 | public void closeRWIO() { |
| 91 | ecriture.close(); |
| 92 | try { |
| 93 | lecture.close(); |
| 94 | } |
| 95 | catch (IOException e) { |
| 96 | System.err.println("IOException: " + e); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | } // fin classe SocketClient |