TD2: Make the client send and receive object message.
[TD_SR.git] / TD2 / client / SocketClient.java
CommitLineData
2c648f75
JB
1import java.io.*;
2import java.net.*;
3import java.util.*;
4
5public 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);
21 closeRWIO();
22 }
2c648f75
JB
23 }
24
25 public SocketClient(String adresseIPServeur, int portServeur) {
26 // établie une connexion au serveur par un appel
27 // à connexionServeur()
e018d1ec 28 attributesInit();
2c648f75 29 try {
e018d1ec 30 connexionServeur(adresseIPServeur, portServeur);
2c648f75
JB
31 }
32 catch (IOException e) {
33 System.err.println("IOException: " + e);
e018d1ec 34 closeRWIO();
2c648f75 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);
2c648f75 43 InputStream IStream = null;
e018d1ec 44 IStream = sock.getInputStream();
2c648f75
JB
45 InputStreamReader IMesg = new InputStreamReader(IStream);
46 lecture = new BufferedReader(IMesg);
c4aaaeca 47 oLecture = new ObjectInputStream(IStream);
2c648f75
JB
48
49 OutputStream OStream = null;
e018d1ec 50 OStream = sock.getOutputStream();
2c648f75 51 ecriture = new PrintWriter(OStream);
c4aaaeca 52 oEcriture = new ObjectOutputStream(OStream);
2c648f75
JB
53 }
54
e018d1ec 55 private void attributesInit() {
c4aaaeca 56 sock = null;
e018d1ec
JB
57 lecture = null;
58 ecriture = null;
c4aaaeca
JB
59 oLecture = null;
60 oEcriture = null;
e018d1ec
JB
61 }
62
2c648f75
JB
63 /**
64 * Send a message on the opened client socket
65 * @param msg a string containing the message to send
66 */
e018d1ec 67 public void sendMsg(String msg) {
2c648f75
JB
68 ecriture.println(msg);
69 ecriture.flush();
2c648f75
JB
70 }
71
c4aaaeca
JB
72 /**
73 * Send an object message on the opened client socket
74 * @param msg a string containing the message to send
75 */
76 public void sendoMsg(Message oMsg) throws IOException {
77 oEcriture.writeObject(oMsg);
78 oEcriture.flush();
79 }
80
2c648f75
JB
81 /**
82 * Receive a message sent on the opened client socket
83 * @return a string containing the received message
84 */
e018d1ec 85 public String receiveMsg() throws IOException {
2c648f75 86 String line = new String();
ef258436 87 //FIXME?: read only the line before the ending newline
e018d1ec 88 line = lecture.readLine();
2c648f75
JB
89 return line;
90 }
91
c4aaaeca
JB
92 /**
93 * Receive an object message sent on the opened client socket
94 * @return a string containing the received message
95 */
96 public Message receiveoMsg() throws IOException, ClassNotFoundException {
97 return (Message)oLecture.readObject();
98 }
99
2c648f75
JB
100 /**
101 * Close all opened I/O streams attached to this object instance
102 */
103 public void closeRWIO() {
2c648f75 104 try {
e018d1ec
JB
105 if (sock != null)
106 sock.close();
107 if (lecture != null)
108 lecture.close();
109 if (ecriture != null)
110 ecriture.close();
c4aaaeca
JB
111 if (oLecture != null)
112 oLecture.close();
113 if (oEcriture != null) {
114 oEcriture.close();
115 }
2c648f75
JB
116 }
117 catch (IOException e) {
118 System.err.println("IOException: " + e);
119 }
120 }
121
122} // fin classe SocketClient