TD2: Make the client send and receive object message.
[TD_SR.git] / TD2 / client / SocketClient.java
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 ObjectInput oLecture;
9 ObjectOutput oEcriture;
10 Socket sock; // le socket client
11
12 public SocketClient() {
13 // établie une connexion au serveur par un appel
14 // à connexionServeur()
15 attributesInit();
16 try {
17 connexionServeur("localhost", 5000);
18 }
19 catch (IOException e) {
20 System.err.println("IOException: " + e);
21 closeRWIO();
22 }
23 }
24
25 public SocketClient(String adresseIPServeur, int portServeur) {
26 // établie une connexion au serveur par un appel
27 // à connexionServeur()
28 attributesInit();
29 try {
30 connexionServeur(adresseIPServeur, portServeur);
31 }
32 catch (IOException e) {
33 System.err.println("IOException: " + e);
34 closeRWIO();
35 }
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);
43 InputStream IStream = null;
44 IStream = sock.getInputStream();
45 InputStreamReader IMesg = new InputStreamReader(IStream);
46 lecture = new BufferedReader(IMesg);
47 oLecture = new ObjectInputStream(IStream);
48
49 OutputStream OStream = null;
50 OStream = sock.getOutputStream();
51 ecriture = new PrintWriter(OStream);
52 oEcriture = new ObjectOutputStream(OStream);
53 }
54
55 private void attributesInit() {
56 sock = null;
57 lecture = null;
58 ecriture = null;
59 oLecture = null;
60 oEcriture = null;
61 }
62
63 /**
64 * Send a message on the opened client socket
65 * @param msg a string containing the message to send
66 */
67 public void sendMsg(String msg) {
68 ecriture.println(msg);
69 ecriture.flush();
70 }
71
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
81 /**
82 * Receive a message sent on the opened client socket
83 * @return a string containing the received message
84 */
85 public String receiveMsg() throws IOException {
86 String line = new String();
87 //FIXME?: read only the line before the ending newline
88 line = lecture.readLine();
89 return line;
90 }
91
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
100 /**
101 * Close all opened I/O streams attached to this object instance
102 */
103 public void closeRWIO() {
104 try {
105 if (sock != null)
106 sock.close();
107 if (lecture != null)
108 lecture.close();
109 if (ecriture != null)
110 ecriture.close();
111 if (oLecture != null)
112 oLecture.close();
113 if (oEcriture != null) {
114 oEcriture.close();
115 }
116 }
117 catch (IOException e) {
118 System.err.println("IOException: " + e);
119 }
120 }
121
122 } // fin classe SocketClient