5 public class ClientSimplifie
{
6 BufferedReader lecture
; // pour le flot d'entrée venant du serveur
7 PrintWriter ecriture
; // pour le flot de sortie vers le serveur
8 Socket sock
; // le socket client
10 public ClientSimplifie() {
11 // établie une connexion au serveur par un appel
12 // à connexionServeur()
15 connexionServeur("localhost", 5000);
17 catch (IOException e
) {
18 System
.err
.println("IOException: " + e
);
23 public ClientSimplifie(String adresseIPServeur
, int portServeur
) {
24 // établie une connexion au serveur par un appel
25 // à connexionServeur()
28 connexionServeur(adresseIPServeur
, portServeur
);
30 catch (IOException e
) {
31 System
.err
.println("IOException: " + e
);
36 private void connexionServeur(String adresseIPServeur
, int portServeur
) throws IOException
{
37 // créer un objet socket lié au socket serveur et l'affecte à sock
38 // puis établie les chaînages de flot nécessaires
39 // pour l'envoi et la reception de messages
40 sock
= new Socket(adresseIPServeur
, portServeur
);
42 OutputStream OStream
= sock
.getOutputStream();
43 ecriture
= new PrintWriter(OStream
);
45 InputStream IStream
= sock
.getInputStream();
46 InputStreamReader IMesg
= new InputStreamReader(IStream
);
47 lecture
= new BufferedReader(IMesg
);
50 private void attributesInit() {
57 * Send a message on the opened client socket
58 * @param msg a string containing the message to send
60 public void sendMsg(String msg
) {
61 ecriture
.println(msg
);
66 * Receive a message sent on the opened client socket
67 * @return a string containing the received message
69 public String
receiveMsg() throws IOException
{
70 String line
= new String();
71 //FIXME?: read only the line before the ending newline
72 line
= lecture
.readLine();
77 * Close all opened I/O streams attached to this object instance
79 public void closeRWIO() {
88 catch (IOException e
) {
89 System
.err
.println("IOException: " + e
);
93 } // fin classe ClientSimplifie