Commit | Line | Data |
---|---|---|
f734b987 JB |
1 | import java.io.*; |
2 | import java.net.*; | |
3 | import java.util.*; | |
4 | ||
5 | public class ClientSimplifie { | |
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 ClientSimplifie() { | |
11 | // établie une connexion au serveur par un appel | |
12 | // à connexionServeur() | |
e018d1ec JB |
13 | attributesInit(); |
14 | try { | |
15 | connexionServeur("localhost", 5000); | |
16 | } | |
17 | catch (IOException e) { | |
18 | System.err.println("IOException: " + e); | |
8c3c3f61 | 19 | e.printStackTrace(); |
e018d1ec JB |
20 | closeRWIO(); |
21 | } | |
f734b987 JB |
22 | } |
23 | ||
24 | public ClientSimplifie(String adresseIPServeur, int portServeur) { | |
25 | // établie une connexion au serveur par un appel | |
26 | // à connexionServeur() | |
e018d1ec | 27 | attributesInit(); |
f734b987 | 28 | try { |
e018d1ec | 29 | connexionServeur(adresseIPServeur, portServeur); |
f734b987 | 30 | } |
7293fe6d | 31 | catch (IOException e) { |
ce28a021 | 32 | System.err.println("IOException: " + e); |
8c3c3f61 | 33 | e.printStackTrace(); |
e018d1ec | 34 | closeRWIO(); |
7293fe6d | 35 | } |
e018d1ec JB |
36 | } |
37 | ||
38 | private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException { | |
39 | // créer un objet socket lié au socket serveur et l'affecte à sock | |
40 | // puis établie les chaînages de flot nécessaires | |
41 | // pour l'envoi et la reception de messages | |
42 | sock = new Socket(adresseIPServeur, portServeur); | |
f734b987 | 43 | |
6b300998 | 44 | OutputStream OStream = sock.getOutputStream(); |
f734b987 | 45 | ecriture = new PrintWriter(OStream); |
6b300998 JB |
46 | |
47 | InputStream IStream = sock.getInputStream(); | |
48 | InputStreamReader IMesg = new InputStreamReader(IStream); | |
49 | lecture = new BufferedReader(IMesg); | |
f734b987 JB |
50 | } |
51 | ||
e018d1ec | 52 | private void attributesInit() { |
c4aaaeca | 53 | sock = null; |
e018d1ec JB |
54 | lecture = null; |
55 | ecriture = null; | |
e018d1ec JB |
56 | } |
57 | ||
7293fe6d JB |
58 | /** |
59 | * Send a message on the opened client socket | |
60 | * @param msg a string containing the message to send | |
61 | */ | |
f734b987 | 62 | public void sendMsg(String msg) { |
4ab67210 | 63 | ecriture.println(msg); |
f734b987 JB |
64 | ecriture.flush(); |
65 | } | |
66 | ||
7293fe6d JB |
67 | /** |
68 | * Receive a message sent on the opened client socket | |
69 | * @return a string containing the received message | |
70 | */ | |
e018d1ec | 71 | public String receiveMsg() throws IOException { |
f734b987 | 72 | String line = new String(); |
ef258436 | 73 | //FIXME?: read only the line before the ending newline |
e018d1ec | 74 | line = lecture.readLine(); |
f734b987 JB |
75 | return line; |
76 | } | |
77 | ||
7293fe6d JB |
78 | /** |
79 | * Close all opened I/O streams attached to this object instance | |
80 | */ | |
f734b987 | 81 | public void closeRWIO() { |
367e2930 | 82 | try { |
e018d1ec JB |
83 | if (sock != null) |
84 | sock.close(); | |
85 | if (lecture != null) | |
86 | lecture.close(); | |
87 | if (ecriture != null) | |
88 | ecriture.close(); | |
367e2930 | 89 | } |
7293fe6d | 90 | catch (IOException e) { |
ce28a021 | 91 | System.err.println("IOException: " + e); |
8c3c3f61 | 92 | e.printStackTrace(); |
7293fe6d | 93 | } |
f734b987 JB |
94 | } |
95 | ||
96 | } // fin classe ClientSimplifie |