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 | |
c4aaaeca JB |
8 | ObjectInput oLecture; |
9 | ObjectOutput oEcriture; | |
2c648f75 JB |
10 | Socket sock; // le socket client |
11 | ||
12 | public SocketClient() { | |
13 | // établie une connexion au serveur par un appel | |
14 | // à connexionServeur() | |
e018d1ec JB |
15 | attributesInit(); |
16 | try { | |
17 | connexionServeur("localhost", 5000); | |
18 | } | |
19 | catch (IOException e) { | |
20 | System.err.println("IOException: " + e); | |
8c3c3f61 | 21 | e.printStackTrace(); |
e018d1ec JB |
22 | closeRWIO(); |
23 | } | |
2c648f75 JB |
24 | } |
25 | ||
26 | public SocketClient(String adresseIPServeur, int portServeur) { | |
27 | // établie une connexion au serveur par un appel | |
28 | // à connexionServeur() | |
e018d1ec | 29 | attributesInit(); |
2c648f75 | 30 | try { |
e018d1ec | 31 | connexionServeur(adresseIPServeur, portServeur); |
2c648f75 JB |
32 | } |
33 | catch (IOException e) { | |
34 | System.err.println("IOException: " + e); | |
8c3c3f61 | 35 | e.printStackTrace(); |
e018d1ec | 36 | closeRWIO(); |
2c648f75 | 37 | } |
e018d1ec JB |
38 | } |
39 | ||
15850b4c | 40 | private void connexionServeur(String adresseIPServeur, int portServeur, boolean hasoStream) throws IOException { |
e018d1ec JB |
41 | // créer un objet socket lié au socket serveur et l'affecte à sock |
42 | // puis établie les chaînages de flot nécessaires | |
43 | // pour l'envoi et la reception de messages | |
44 | sock = new Socket(adresseIPServeur, portServeur); | |
2c648f75 | 45 | |
da31e6da | 46 | OutputStream OStream = sock.getOutputStream(); |
2c648f75 | 47 | ecriture = new PrintWriter(OStream); |
15850b4c JB |
48 | if (hasoStream) |
49 | oEcriture = new ObjectOutputStream(OStream); | |
da31e6da JB |
50 | |
51 | InputStream IStream = sock.getInputStream(); | |
52 | InputStreamReader IMesg = new InputStreamReader(IStream); | |
53 | lecture = new BufferedReader(IMesg); | |
15850b4c JB |
54 | if (hasoStream) |
55 | oLecture = new ObjectInputStream(IStream); | |
56 | } | |
57 | ||
58 | private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException { | |
59 | // créer un objet socket lié au socket serveur et l'affecte à sock | |
60 | // puis établie les chaînages de flot nécessaires | |
61 | // pour l'envoi et la reception de messages | |
62 | connexionServeur(adresseIPServeur, portServeur, false); | |
2c648f75 JB |
63 | } |
64 | ||
e018d1ec | 65 | private void attributesInit() { |
c4aaaeca | 66 | sock = null; |
e018d1ec JB |
67 | lecture = null; |
68 | ecriture = null; | |
c4aaaeca JB |
69 | oLecture = null; |
70 | oEcriture = null; | |
e018d1ec JB |
71 | } |
72 | ||
2c648f75 JB |
73 | /** |
74 | * Send a message on the opened client socket | |
75 | * @param msg a string containing the message to send | |
76 | */ | |
e018d1ec | 77 | public void sendMsg(String msg) { |
2c648f75 JB |
78 | ecriture.println(msg); |
79 | ecriture.flush(); | |
2c648f75 JB |
80 | } |
81 | ||
c4aaaeca JB |
82 | /** |
83 | * Send an object message on the opened client socket | |
84 | * @param msg a string containing the message to send | |
85 | */ | |
86 | public void sendoMsg(Message oMsg) throws IOException { | |
87 | oEcriture.writeObject(oMsg); | |
88 | oEcriture.flush(); | |
89 | } | |
90 | ||
2c648f75 JB |
91 | /** |
92 | * Receive a message sent on the opened client socket | |
93 | * @return a string containing the received message | |
94 | */ | |
e018d1ec | 95 | public String receiveMsg() throws IOException { |
2c648f75 | 96 | String line = new String(); |
ef258436 | 97 | //FIXME?: read only the line before the ending newline |
e018d1ec | 98 | line = lecture.readLine(); |
2c648f75 JB |
99 | return line; |
100 | } | |
101 | ||
c4aaaeca JB |
102 | /** |
103 | * Receive an object message sent on the opened client socket | |
104 | * @return a string containing the received message | |
105 | */ | |
106 | public Message receiveoMsg() throws IOException, ClassNotFoundException { | |
107 | return (Message)oLecture.readObject(); | |
108 | } | |
109 | ||
2c648f75 JB |
110 | /** |
111 | * Close all opened I/O streams attached to this object instance | |
112 | */ | |
113 | public void closeRWIO() { | |
2c648f75 | 114 | try { |
e018d1ec JB |
115 | if (sock != null) |
116 | sock.close(); | |
117 | if (lecture != null) | |
118 | lecture.close(); | |
119 | if (ecriture != null) | |
120 | ecriture.close(); | |
c4aaaeca JB |
121 | if (oLecture != null) |
122 | oLecture.close(); | |
123 | if (oEcriture != null) { | |
124 | oEcriture.close(); | |
125 | } | |
2c648f75 JB |
126 | } |
127 | catch (IOException e) { | |
128 | System.err.println("IOException: " + e); | |
8c3c3f61 | 129 | e.printStackTrace(); |
2c648f75 JB |
130 | } |
131 | } | |
132 | ||
133 | } // fin classe SocketClient |