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