b038475de13bedc2abdd6109610cc9a7459d83d9
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
9 ObjectOutput oEcriture
;
10 Socket sock
; // le socket client
12 public SocketClient() {
13 // établie une connexion au serveur par un appel
14 // à connexionServeur()
17 connexionServeur("localhost", 5000);
19 catch (IOException e
) {
20 System
.err
.println("IOException: " + e
);
26 public SocketClient(boolean withoStream
) {
27 // établie une connexion au serveur par un appel
28 // à connexionServeur()
31 connexionServeur("localhost", 5000, withoStream
);
33 catch (IOException e
) {
34 System
.err
.println("IOException: " + e
);
40 public SocketClient(String adresseIPServeur
, int portServeur
) {
41 // établie une connexion au serveur par un appel
42 // à connexionServeur()
45 connexionServeur(adresseIPServeur
, portServeur
);
47 catch (IOException e
) {
48 System
.err
.println("IOException: " + e
);
54 private void connexionServeur(String adresseIPServeur
, int portServeur
, boolean hasoStream
) throws IOException
{
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
);
60 OutputStream OStream
= sock
.getOutputStream();
61 ecriture
= new PrintWriter(OStream
);
63 oEcriture
= new ObjectOutputStream(OStream
);
65 InputStream IStream
= sock
.getInputStream();
66 InputStreamReader IMesg
= new InputStreamReader(IStream
);
67 lecture
= new BufferedReader(IMesg
);
69 oLecture
= new ObjectInputStream(IStream
);
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);
79 private void attributesInit() {
88 * Send a message on the opened client socket
89 * @param msg a string containing the message to send
91 public void sendMsg(String msg
) {
92 ecriture
.println(msg
);
97 * Send an object message on the opened client socket
98 * @param oMsg an object containing the message to send
100 public void sendoMsg(Message oMsg
) throws IOException
{
101 oEcriture
.writeObject(oMsg
);
106 * Receive a message sent on the opened client socket
107 * @return a string containing the received message
109 public String
receiveMsg() throws IOException
{
110 String line
= new String();
111 //FIXME?: read only the line before the ending newline
112 line
= lecture
.readLine();
117 * Receive an object message sent on the opened client socket
118 * @return a object containing the received message
120 public Message
receiveoMsg() throws IOException
, ClassNotFoundException
{
121 return (Message
)oLecture
.readObject();
125 * Close all opened I/O streams attached to this object instance
127 public void closeRWIO() {
133 if (ecriture
!= null)
135 if (oLecture
!= null)
137 if (oEcriture
!= null) {
141 catch (IOException e
) {
142 System
.err
.println("IOException: " + e
);
147 } // fin classe SocketClient