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