}
private void attributesInit() {
+ sock = null;
lecture = null;
ecriture = null;
- sock = null;
}
/**
}
}
- public static void main (String[] args) {
+ /**
+ * main for text based message broadcasting
+ */
+ public static void main2() {
SocketClient client = null;
Thread thS = null;
Thread thR = null;
}
}
+ public static void main(String[] args) {
+ SocketClient client = null;
+ Thread thS = null;
+ Thread thR = null;
+
+ try {
+ client = new SocketClient();
+ thS = new Thread(new ThreadClientoSend(client));
+ thR = new Thread(new ThreadClientoReceive(client));
+ thS.setName("thoS");
+ thS.start();
+ thR.setName("thoR");
+ thR.start();
+ }
+ 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();
+ }
+ }
+
}
# NAME = Camilo Juan
CLASSES = \
+ Message.java \
ClientSimplifie.java \
SocketClient.java \
ThreadClientSend.java \
ThreadClientReceive.java \
+ ThreadClientoSend.java \
+ ThreadClientoReceive.java \
Main.java
#
--- /dev/null
+import java.io.Serializable;
+import java.util.Calendar;
+
+public class Message implements Serializable {
+ // L'emeteur du message
+ private String emetteur ;
+ // Le contenu du message
+ private String texte ;
+ // Heure du message
+ private Calendar heure ;
+
+ // Les méthodes
+
+ Message(String name, String msg) {
+ emetteur = name;
+ texte = msg;
+ heure = Calendar.getInstance();
+ }
+
+ /**
+ * @param name the emetteur to set
+ */
+ public void setEmetteur(String name) {
+ emetteur = name;
+ }
+
+ /**
+ * @return the emetteur
+ */
+ public String getEmetteur() {
+ return emetteur;
+ }
+
+ /**
+ * @param texte the texte to set
+ */
+ public void setTexte(String texte) {
+ this.texte = texte;
+ }
+
+ /**
+ * @return the texte
+ */
+ public String getTexte() {
+ return texte;
+ }
+
+ /**
+ * @param heure the heure to set
+ */
+ public void setHeure(Calendar heure) {
+ this.heure = heure;
+ }
+
+ /**
+ * @return the heure
+ */
+ public Calendar getHeure() {
+ return heure;
+ }
+
+ public String toString() {
+ return "<" + emetteur + ":" + heure + "> " + texte;
+ }
+}
public class SocketClient {
BufferedReader lecture; // pour le flot d'entrée venant du serveur
PrintWriter ecriture; // pour le flot de sortie vers le serveur
+ ObjectInput oLecture;
+ ObjectOutput oEcriture;
Socket sock; // le socket client
public SocketClient() {
IStream = sock.getInputStream();
InputStreamReader IMesg = new InputStreamReader(IStream);
lecture = new BufferedReader(IMesg);
+ oLecture = new ObjectInputStream(IStream);
OutputStream OStream = null;
OStream = sock.getOutputStream();
ecriture = new PrintWriter(OStream);
+ oEcriture = new ObjectOutputStream(OStream);
}
private void attributesInit() {
+ sock = null;
lecture = null;
ecriture = null;
- sock = null;
+ oLecture = null;
+ oEcriture = null;
}
/**
ecriture.flush();
}
+ /**
+ * Send an object message on the opened client socket
+ * @param msg a string containing the message to send
+ */
+ public void sendoMsg(Message oMsg) throws IOException {
+ oEcriture.writeObject(oMsg);
+ oEcriture.flush();
+ }
+
/**
* Receive a message sent on the opened client socket
* @return a string containing the received message
return line;
}
+ /**
+ * Receive an object message sent on the opened client socket
+ * @return a string containing the received message
+ */
+ public Message receiveoMsg() throws IOException, ClassNotFoundException {
+ return (Message)oLecture.readObject();
+ }
+
/**
* Close all opened I/O streams attached to this object instance
*/
lecture.close();
if (ecriture != null)
ecriture.close();
+ if (oLecture != null)
+ oLecture.close();
+ if (oEcriture != null) {
+ oEcriture.close();
+ }
}
catch (IOException e) {
System.err.println("IOException: " + e);
public void run() {
try {
boolean end = false;
- //FIXME: Not exiting properly randomly from that loop!
+ //FIXME: not exiting properly randomly from that loop!
while (!end) {
String rline = client.receiveMsg();
if (rline.equals(".")) {
--- /dev/null
+import java.io.*;
+
+public class ThreadClientoReceive implements Runnable {
+ private SocketClient client;
+
+ ThreadClientoReceive(SocketClient c) {
+ client = c;
+ }
+
+ public void run() {
+ try {
+ boolean end = false;
+ //FIXME: not exiting properly randomly from that loop!
+ while (!end) {
+ Message roMsg = client.receiveoMsg();
+ if (roMsg.getTexte().equals(".")) {
+ end = true;
+ }
+ System.out.println(Thread.currentThread().getName() + " a recu " + roMsg);
+ }
+ }
+ catch (IOException e) {
+ System.err.println("IOException: " + e);
+ e.printStackTrace();
+ }
+ catch (ClassNotFoundException e) {
+ System.err.println("ClassNotFoundException: " + e);
+ e.printStackTrace();
+ }
+ }
+}
--- /dev/null
+import java.io.*;
+
+public class ThreadClientoSend implements Runnable {
+ private SocketClient client;
+
+ ThreadClientoSend(SocketClient c) {
+ client = c;
+ }
+
+ public void run() {
+ 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;
+ }
+ Message oMsg = new Message("Name", line);
+ client.sendoMsg(oMsg);
+ System.out.println(Thread.currentThread().getName() + " a envoye " + oMsg);
+ }
+ }
+ 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);
+ }
+ }
+ }
+ }
+}
# NAME = Camilo Juan
CLASSES = \
+ Message.java \
BroadcastThreadService.java \
Main.java