Integer val;
while (true) {
val = (Integer)buffer.preleve();
- System.out.println (Thread.currentThread().getName() + " a preleve " + val);
+ System.out.println(Thread.currentThread().getName() + " a preleve " + val);
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1001));
}
public static void main (String[] args) {
- //FIXME: Implement the args parsing to set this tree values dynamically
+ //FIXME: Implement the args parsing to set the three values dynamically
final int BUFFER_SIZE = 1;
final int PROD_NUMBER = 20;
final int CONS_NUMBER = 20;
public void run() {
while (true) {
buffer.depose(new Integer(val));
- System.out.println (Thread.currentThread().getName() + " a depose " + val);
+ System.out.println(Thread.currentThread().getName() + " a depose " + val);
val++;
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(101));
public ClientSimplifie() {
// établie une connexion au serveur par un appel
// à connexionServeur()
- connexionServeur("localhost", 5000);
+ attributesInit();
+ try {
+ connexionServeur("localhost", 5000);
+ }
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ closeRWIO();
+ }
}
public ClientSimplifie(String adresseIPServeur, int portServeur) {
// établie une connexion au serveur par un appel
// à connexionServeur()
- connexionServeur(adresseIPServeur, portServeur);
- }
-
- private void connexionServeur(String adresseIPServeur, int portServeur) {
- // créer un objet socket lié au socket serveur et l'affecte à sock
- // puis établie les chaînages de flot nécessaires
- // pour l'envoi et la reception de messages
+ attributesInit();
try {
- sock = new Socket(adresseIPServeur, portServeur);
+ connexionServeur(adresseIPServeur, portServeur);
}
catch (IOException e) {
System.err.println("IOException: " + e);
+ closeRWIO();
}
+ }
+
+ private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException {
+ // créer un objet socket lié au socket serveur et l'affecte à sock
+ // puis établie les chaînages de flot nécessaires
+ // pour l'envoi et la reception de messages
+ sock = new Socket(adresseIPServeur, portServeur);
InputStream IStream = null;
- try {
- IStream = sock.getInputStream();
- }
- catch (IOException e) {
- System.err.println("IOException: " + e);
- }
+ IStream = sock.getInputStream();
InputStreamReader IMesg = new InputStreamReader(IStream);
lecture = new BufferedReader(IMesg);
OutputStream OStream = null;
- try {
- OStream = sock.getOutputStream();
- }
- catch (IOException e) {
- System.err.println("IOException: " + e);
- }
+ OStream = sock.getOutputStream();
ecriture = new PrintWriter(OStream);
}
+ private void attributesInit() {
+ lecture = null;
+ ecriture = null;
+ sock = null;
+ }
+
/**
* Send a message on the opened client socket
* @param msg a string containing the message to send
* Receive a message sent on the opened client socket
* @return a string containing the received message
*/
- public String receiveMsg() {
+ public String receiveMsg() throws IOException {
String line = new String();
- try {
- //FIXME: read only the line before the ending newline
- line = lecture.readLine();
- }
- catch (IOException e) {
- System.err.println("IOException: " + e);
- }
+ //FIXME: read only the line before the ending newline
+ line = lecture.readLine();
return line;
}
* Close all opened I/O streams attached to this object instance
*/
public void closeRWIO() {
- ecriture.close();
try {
- lecture.close();
+ if (sock != null)
+ sock.close();
+ if (lecture != null)
+ lecture.close();
+ if (ecriture != null)
+ ecriture.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
* Main for testing ClientSimplifie
*/
private static void main1() {
- SocketClient client = new SocketClient();
+ ClientSimplifie client = null;
+ BufferedReader userInput = null;
try {
- client.sendMsg("Line1");
- String msg = client.receiveMsg();
- System.out.println(msg);
+ client = new ClientSimplifie();
+ userInput = new BufferedReader(new InputStreamReader(System.in));
+ boolean end = false;
+ while (!end) {
+ String line = userInput.readLine();
+ if (line.equals("."))
+ end = true;
+ client.sendMsg(line);
+ String rline = client.receiveMsg();
+ System.out.println(rline);
+ }
}
- catch (Exception e) {
- System.err.println("Exception: " + e);
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
}
finally {
client.closeRWIO();
+ if (userInput != null) {
+ try {
+ userInput.close();
+ }
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ }
+ }
}
}
-
public static void main (String[] args) {
- SocketClient client = new SocketClient();
+ SocketClient client = null;
+ Thread thS = null;
+ Thread thR = null;
- // try {
- ThreadClientSend thCS = new ThreadClientSend(client);
- //FIXME: Implement a loop based on user input to set dynamically the message to sent.
- //for (int i = 0; i < 10; i++) {
- // thCS.setMsg("Line" + i);
- //}
- thCS.setMsg("Line1");
- Thread thS = new Thread(thCS);
- Thread thR = new Thread(new ThreadClientReceive(client));
+ try {
+ client = new SocketClient();
+ thS = new Thread(new ThreadClientSend(client));
+ thR = new Thread(new ThreadClientReceive(client));
thS.setName("thS");
thS.start();
thR.setName("thR");
thR.start();
- // }
- // catch (Exception e) {
- // System.err.println("Exception: " + e);
- // }
- // finally {
- // client.closeRWIO();
- // }
-
+ }
+ catch (Exception e) {
+ System.err.println("Exception: " + e);
+ }
+ finally {
+ try {
+ thS.join();
+ thR.join();
+ }
+ catch (InterruptedException e) {
+ System.err.println("InterruptedException: " + e);
+ e.printStackTrace();
+ }
+ client.closeRWIO();
+ }
}
}
public SocketClient() {
// établie une connexion au serveur par un appel
// à connexionServeur()
- connexionServeur("localhost", 5000);
+ attributesInit();
+ try {
+ connexionServeur("localhost", 5000);
+ }
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ closeRWIO();
+ }
}
public SocketClient(String adresseIPServeur, int portServeur) {
// établie une connexion au serveur par un appel
// à connexionServeur()
- connexionServeur(adresseIPServeur, portServeur);
- }
-
- private void connexionServeur(String adresseIPServeur, int portServeur) {
- // créer un objet socket lié au socket serveur et l'affecte à sock
- // puis établie les chaînages de flot nécessaires
- // pour l'envoi et la reception de messages
+ attributesInit();
try {
- sock = new Socket(adresseIPServeur, portServeur);
+ connexionServeur(adresseIPServeur, portServeur);
}
catch (IOException e) {
System.err.println("IOException: " + e);
+ closeRWIO();
}
+ }
+
+ private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException {
+ // créer un objet socket lié au socket serveur et l'affecte à sock
+ // puis établie les chaînages de flot nécessaires
+ // pour l'envoi et la reception de messages
+ sock = new Socket(adresseIPServeur, portServeur);
InputStream IStream = null;
- try {
- IStream = sock.getInputStream();
- }
- catch (IOException e) {
- System.err.println("IOException: " + e);
- }
+ IStream = sock.getInputStream();
InputStreamReader IMesg = new InputStreamReader(IStream);
lecture = new BufferedReader(IMesg);
OutputStream OStream = null;
- try {
- OStream = sock.getOutputStream();
- }
- catch (IOException e) {
- System.err.println("IOException: " + e);
- }
+ OStream = sock.getOutputStream();
ecriture = new PrintWriter(OStream);
}
+ private void attributesInit() {
+ lecture = null;
+ ecriture = null;
+ sock = null;
+ }
+
/**
* Send a message on the opened client socket
* @param msg a string containing the message to send
*/
- public synchronized void sendMsg(String msg) {
- //NOTE: it's not really required with one socket writer thread.
- while (msg.isEmpty()) {
- try {
- wait();
- }
- catch (InterruptedException e) {
- System.err.println("InterruptedException: " + e);
- }
- }
+ public void sendMsg(String msg) {
ecriture.println(msg);
ecriture.flush();
- notifyAll();
}
/**
* Receive a message sent on the opened client socket
* @return a string containing the received message
*/
- public String receiveMsg() {
+ public String receiveMsg() throws IOException {
String line = new String();
- try {
- //FIXME: read only the line before the ending newline
- line = lecture.readLine();
- }
- catch (IOException e) {
- System.err.println("IOException: " + e);
- }
+ //FIXME: read only the line before the ending newline
+ line = lecture.readLine();
return line;
}
* Close all opened I/O streams attached to this object instance
*/
public void closeRWIO() {
- ecriture.close();
try {
- lecture.close();
+ if (sock != null)
+ sock.close();
+ if (lecture != null)
+ lecture.close();
+ if (ecriture != null)
+ ecriture.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
import java.util.concurrent.ThreadLocalRandom;
+import java.io.*;
public class ThreadClientReceive implements Runnable {
private SocketClient client;
- private String msg = new String();
ThreadClientReceive(SocketClient c) {
client = c;
}
- /**
- * @return the msg
- */
- public String getMsg() {
- return msg;
- }
-
public void run() {
- while (true) {
- try {
- msg = client.receiveMsg();
- System.out.println (Thread.currentThread().getName() + " a recu " + msg);
- try {
- Thread.sleep(ThreadLocalRandom.current().nextInt(101));
- }
- catch (InterruptedException e) {
- System.err.println("InterruptedException: " + e);
- }
- }
- catch (Exception e) {
- System.err.println("Exception: " + e);
+ try {
+ boolean end = false;
+ while (!end) {
+ String rline = client.receiveMsg();
+ if (rline.equals(".")) {
+ end = true;
+ }
+ System.out.println(Thread.currentThread().getName() + " a recu " + rline);
}
}
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ e.printStackTrace();
+ }
}
}
import java.util.concurrent.ThreadLocalRandom;
+import java.io.*;
public class ThreadClientSend implements Runnable {
private SocketClient client;
- private String msg = new String();
ThreadClientSend(SocketClient c) {
client = c;
}
- /**
- * Message to sent setter
- * @param m the text message to sent
- */
- public void setMsg(String m) {
- msg = m;
- }
-
public void run() {
- while (true) {
- try {
- client.sendMsg(msg);
- System.out.println (Thread.currentThread().getName() + " a envoye " + msg);
- try {
- Thread.sleep(ThreadLocalRandom.current().nextInt(101));
- }
- catch (InterruptedException e) {
- System.err.println("InterruptedException: " + e);
- }
- }
- catch (Exception e) {
- System.err.println("Exception: " + e);
+ BufferedReader userInput = null;
+ try {
+ userInput = new BufferedReader(new InputStreamReader(System.in));
+ boolean end = false;
+ while (!end) {
+ String line = userInput.readLine();
+ if (line.equals(".")) {
+ end = true;
+ }
+ client.sendMsg(line);
+ System.out.println(Thread.currentThread().getName() + " a envoye " + line);
}
+ }
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ e.printStackTrace();
+ }
+ finally {
+ if (userInput != null) {
+ try {
+ userInput.close();
+ }
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ }
+ }
}
}
}