3c6d021c605f3a22a7d9537d8f1bf830a28854f7
[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
44 OutputStream OStream = sock.getOutputStream();
45 ecriture = new PrintWriter(OStream);
46 oEcriture = new ObjectOutputStream(OStream);
47
48 InputStream IStream = sock.getInputStream();
49 InputStreamReader IMesg = new InputStreamReader(IStream);
50 lecture = new BufferedReader(IMesg);
51 oLecture = new ObjectInputStream(IStream);
52 }
53
54 private void attributesInit() {
55 sock = null;
56 lecture = null;
57 ecriture = null;
58 oLecture = null;
59 oEcriture = null;
60 }
61
62 /**
63 * Send a message on the opened client socket
64 * @param msg a string containing the message to send
65 */
66 public void sendMsg(String msg) {
67 ecriture.println(msg);
68 ecriture.flush();
69 }
70
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
80 /**
81 * Receive a message sent on the opened client socket
82 * @return a string containing the received message
83 */
84 public String receiveMsg() throws IOException {
85 String line = new String();
86 //FIXME?: read only the line before the ending newline
87 line = lecture.readLine();
88 return line;
89 }
90
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
99 /**
100 * Close all opened I/O streams attached to this object instance
101 */
102 public void closeRWIO() {
103 try {
104 if (sock != null)
105 sock.close();
106 if (lecture != null)
107 lecture.close();
108 if (ecriture != null)
109 ecriture.close();
110 if (oLecture != null)
111 oLecture.close();
112 if (oEcriture != null) {
113 oEcriture.close();
114 }
115 }
116 catch (IOException e) {
117 System.err.println("IOException: " + e);
118 }
119 }
120
121 } // fin classe SocketClient