TD2: Fix all important bugs in the object passing server and client
[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
da31e6da 44 OutputStream OStream = sock.getOutputStream();
2c648f75 45 ecriture = new PrintWriter(OStream);
c4aaaeca 46 oEcriture = new ObjectOutputStream(OStream);
da31e6da
JB
47
48 InputStream IStream = sock.getInputStream();
49 InputStreamReader IMesg = new InputStreamReader(IStream);
50 lecture = new BufferedReader(IMesg);
51 oLecture = new ObjectInputStream(IStream);
2c648f75
JB
52 }
53
e018d1ec 54 private void attributesInit() {
c4aaaeca 55 sock = null;
e018d1ec
JB
56 lecture = null;
57 ecriture = null;
c4aaaeca
JB
58 oLecture = null;
59 oEcriture = null;
e018d1ec
JB
60 }
61
2c648f75
JB
62 /**
63 * Send a message on the opened client socket
64 * @param msg a string containing the message to send
65 */
e018d1ec 66 public void sendMsg(String msg) {
2c648f75
JB
67 ecriture.println(msg);
68 ecriture.flush();
2c648f75
JB
69 }
70
c4aaaeca
JB
71 /**
72 * Send an object message on the opened client socket
73 * @param msg a string containing the message to send
74 */
75 public void sendoMsg(Message oMsg) throws IOException {
76 oEcriture.writeObject(oMsg);
77 oEcriture.flush();
78 }
79
2c648f75
JB
80 /**
81 * Receive a message sent on the opened client socket
82 * @return a string containing the received message
83 */
e018d1ec 84 public String receiveMsg() throws IOException {
2c648f75 85 String line = new String();
ef258436 86 //FIXME?: read only the line before the ending newline
e018d1ec 87 line = lecture.readLine();
2c648f75
JB
88 return line;
89 }
90
c4aaaeca
JB
91 /**
92 * Receive an object message sent on the opened client socket
93 * @return a string containing the received message
94 */
95 public Message receiveoMsg() throws IOException, ClassNotFoundException {
96 return (Message)oLecture.readObject();
97 }
98
2c648f75
JB
99 /**
100 * Close all opened I/O streams attached to this object instance
101 */
102 public void closeRWIO() {
2c648f75 103 try {
e018d1ec
JB
104 if (sock != null)
105 sock.close();
106 if (lecture != null)
107 lecture.close();
108 if (ecriture != null)
109 ecriture.close();
c4aaaeca
JB
110 if (oLecture != null)
111 oLecture.close();
112 if (oEcriture != null) {
113 oEcriture.close();
114 }
2c648f75
JB
115 }
116 catch (IOException e) {
117 System.err.println("IOException: " + e);
118 }
119 }
120
121} // fin classe SocketClient