Commit | Line | Data |
---|---|---|
2c648f75 JB |
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 | |
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); | |
8c3c3f61 | 21 | e.printStackTrace(); |
e018d1ec JB |
22 | closeRWIO(); |
23 | } | |
2c648f75 JB |
24 | } |
25 | ||
1a8b0826 JB |
26 | public SocketClient(boolean withoStream) { |
27 | // établie une connexion au serveur par un appel | |
28 | // à connexionServeur() | |
29 | attributesInit(); | |
30 | try { | |
31 | connexionServeur("localhost", 5000, withoStream); | |
32 | } | |
33 | catch (IOException e) { | |
34 | System.err.println("IOException: " + e); | |
35 | e.printStackTrace(); | |
36 | closeRWIO(); | |
37 | } | |
38 | } | |
39 | ||
2c648f75 JB |
40 | public SocketClient(String adresseIPServeur, int portServeur) { |
41 | // établie une connexion au serveur par un appel | |
42 | // à connexionServeur() | |
e018d1ec | 43 | attributesInit(); |
2c648f75 | 44 | try { |
e018d1ec | 45 | connexionServeur(adresseIPServeur, portServeur); |
2c648f75 JB |
46 | } |
47 | catch (IOException e) { | |
48 | System.err.println("IOException: " + e); | |
8c3c3f61 | 49 | e.printStackTrace(); |
e018d1ec | 50 | closeRWIO(); |
2c648f75 | 51 | } |
e018d1ec JB |
52 | } |
53 | ||
15850b4c | 54 | private void connexionServeur(String adresseIPServeur, int portServeur, boolean hasoStream) throws IOException { |
e018d1ec JB |
55 | // créer un objet socket lié au socket serveur et l'affecte à sock |
56 | // puis établie les chaînages de flot nécessaires | |
57 | // pour l'envoi et la reception de messages | |
58 | sock = new Socket(adresseIPServeur, portServeur); | |
2c648f75 | 59 | |
da31e6da | 60 | OutputStream OStream = sock.getOutputStream(); |
2c648f75 | 61 | ecriture = new PrintWriter(OStream); |
15850b4c JB |
62 | if (hasoStream) |
63 | oEcriture = new ObjectOutputStream(OStream); | |
da31e6da JB |
64 | |
65 | InputStream IStream = sock.getInputStream(); | |
66 | InputStreamReader IMesg = new InputStreamReader(IStream); | |
67 | lecture = new BufferedReader(IMesg); | |
15850b4c JB |
68 | if (hasoStream) |
69 | oLecture = new ObjectInputStream(IStream); | |
70 | } | |
71 | ||
72 | private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException { | |
73 | // créer un objet socket lié au socket serveur et l'affecte à sock | |
74 | // puis établie les chaînages de flot nécessaires | |
75 | // pour l'envoi et la reception de messages | |
76 | connexionServeur(adresseIPServeur, portServeur, false); | |
2c648f75 JB |
77 | } |
78 | ||
e018d1ec | 79 | private void attributesInit() { |
c4aaaeca | 80 | sock = null; |
e018d1ec JB |
81 | lecture = null; |
82 | ecriture = null; | |
c4aaaeca JB |
83 | oLecture = null; |
84 | oEcriture = null; | |
e018d1ec JB |
85 | } |
86 | ||
2c648f75 JB |
87 | /** |
88 | * Send a message on the opened client socket | |
89 | * @param msg a string containing the message to send | |
90 | */ | |
e018d1ec | 91 | public void sendMsg(String msg) { |
2c648f75 JB |
92 | ecriture.println(msg); |
93 | ecriture.flush(); | |
2c648f75 JB |
94 | } |
95 | ||
c4aaaeca JB |
96 | /** |
97 | * Send an object message on the opened client socket | |
f8c647b5 | 98 | * @param oMsg an object containing the message to send |
c4aaaeca JB |
99 | */ |
100 | public void sendoMsg(Message oMsg) throws IOException { | |
101 | oEcriture.writeObject(oMsg); | |
102 | oEcriture.flush(); | |
103 | } | |
104 | ||
2c648f75 JB |
105 | /** |
106 | * Receive a message sent on the opened client socket | |
107 | * @return a string containing the received message | |
108 | */ | |
e018d1ec | 109 | public String receiveMsg() throws IOException { |
2c648f75 | 110 | String line = new String(); |
ef258436 | 111 | //FIXME?: read only the line before the ending newline |
e018d1ec | 112 | line = lecture.readLine(); |
2c648f75 JB |
113 | return line; |
114 | } | |
115 | ||
c4aaaeca JB |
116 | /** |
117 | * Receive an object message sent on the opened client socket | |
f8c647b5 | 118 | * @return a object containing the received message |
c4aaaeca JB |
119 | */ |
120 | public Message receiveoMsg() throws IOException, ClassNotFoundException { | |
121 | return (Message)oLecture.readObject(); | |
122 | } | |
123 | ||
2c648f75 JB |
124 | /** |
125 | * Close all opened I/O streams attached to this object instance | |
126 | */ | |
127 | public void closeRWIO() { | |
2c648f75 | 128 | try { |
e018d1ec JB |
129 | if (sock != null) |
130 | sock.close(); | |
131 | if (lecture != null) | |
132 | lecture.close(); | |
133 | if (ecriture != null) | |
134 | ecriture.close(); | |
c4aaaeca JB |
135 | if (oLecture != null) |
136 | oLecture.close(); | |
137 | if (oEcriture != null) { | |
138 | oEcriture.close(); | |
139 | } | |
2c648f75 JB |
140 | } |
141 | catch (IOException e) { | |
142 | System.err.println("IOException: " + e); | |
8c3c3f61 | 143 | e.printStackTrace(); |
2c648f75 JB |
144 | } |
145 | } | |
146 | ||
147 | } // fin classe SocketClient |