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
);
25 public SocketClient(String adresseIPServeur
, int portServeur
) {
26 // établie une connexion au serveur par un appel
27 // à connexionServeur()
30 connexionServeur(adresseIPServeur
, portServeur
);
32 catch (IOException e
) {
33 System
.err
.println("IOException: " + e
);
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
);
49 OutputStream OStream
= null;
50 OStream
= sock
.getOutputStream();
51 ecriture
= new PrintWriter(OStream
);
52 oEcriture
= new ObjectOutputStream(OStream
);
55 private void attributesInit() {
64 * Send a message on the opened client socket
65 * @param msg a string containing the message to send
67 public void sendMsg(String msg
) {
68 ecriture
.println(msg
);
73 * Send an object message on the opened client socket
74 * @param msg a string containing the message to send
76 public void sendoMsg(Message oMsg
) throws IOException
{
77 oEcriture
.writeObject(oMsg
);
82 * Receive a message sent on the opened client socket
83 * @return a string containing the received message
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();
93 * Receive an object message sent on the opened client socket
94 * @return a string containing the received message
96 public Message
receiveoMsg() throws IOException
, ClassNotFoundException
{
97 return (Message
)oLecture
.readObject();
101 * Close all opened I/O streams attached to this object instance
103 public void closeRWIO() {
109 if (ecriture
!= null)
111 if (oLecture
!= null)
113 if (oEcriture
!= null) {
117 catch (IOException e
) {
118 System
.err
.println("IOException: " + e
);
122 } // fin classe SocketClient