Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
- * Main for testing ClientSimplifie
+ * main for testing ClientSimplifie
*/
private static void main1() {
ClientSimplifie client = null;
*/
private static void main1() {
ClientSimplifie client = null;
-import java.util.concurrent.ThreadLocalRandom;
import java.io.*;
public class ThreadClientReceive implements Runnable {
import java.io.*;
public class ThreadClientReceive implements Runnable {
public void run() {
try {
boolean end = false;
public void run() {
try {
boolean end = false;
- //FIXME: Not exiting properly from that loop!
+ //FIXME: Not exiting properly randomly from that loop!
while (!end) {
String rline = client.receiveMsg();
if (rline.equals(".")) {
while (!end) {
String rline = client.receiveMsg();
if (rline.equals(".")) {
-import java.util.concurrent.ThreadLocalRandom;
import java.io.*;
public class ThreadClientSend implements Runnable {
import java.io.*;
public class ThreadClientSend implements Runnable {
-Lors d'un accès simultané au server sur le même port de deux ClientSimplifie, on
+Le ClientSimplifie ne peut envoyer et recevoir en même temps.
+
+De plus lors d'un accès vraiment simultané au server sur le même port de deux ClientSimplifie, on
soulève une exception sur le serveur :
java.util.ConcurrentModificationException
soulève une exception sur le serveur :
java.util.ConcurrentModificationException
import java.io.*;
import java.util.*;
import java.io.*;
import java.util.*;
-public class EchoServerThreadService implements Runnable {
+public class BroadcastThreadService implements Runnable {
private Socket clientSocket;
private Socket clientSocket;
- private ArrayList<PrintWriter> listWriter;
+ private static ArrayList<PrintWriter> listWriter;
- EchoServerThreadService(Socket clientSocket) {
- System.out.println("Creation d'un thread pour repondre a un client, port " + clientSocket.getPort());
+ BroadcastThreadService(Socket clientSocket) {
+ System.out.println("Creation d'un thread pour repondre a un client, port "
+ + clientSocket.getPort());
this.clientSocket = clientSocket;
this.clientSocket = clientSocket;
+ if (listWriter == null)
+ listWriter = new ArrayList<PrintWriter>();
}
public void run() {
try {
}
public void run() {
try {
- doService(clientSocket);
+ doService(clientSocket, listWriter);
+ //FIXME: also close the BR and PW?
+ //FIXME: remove the associated PW from the ArrayList
} catch (IOException e) {
System.err.println("IOException : " + e);
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException : " + e);
e.printStackTrace();
- public void doService(Socket clientSocket) throws IOException {
+ public void doService(Socket clientSocket, ArrayList<PrintWriter> sharedList) throws IOException {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- out = new PrintStream(clientSocket.getOutputStream());
- //listWriter.add(new PrintWriter(clientSocket.getOutputStream()));
+ sharedList.add(new PrintWriter(clientSocket.getOutputStream()));
boolean end = false;
while (!end) {
String theLine = in.readLine();
if (theLine.equals("."))
end = true; // le thread de service doit terminer
boolean end = false;
while (!end) {
String theLine = in.readLine();
if (theLine.equals("."))
end = true; // le thread de service doit terminer
}
System.out.println("Fin du thread repondant au client, port "
+ clientSocket.getPort());
}
}
System.out.println("Fin du thread repondant au client, port "
+ clientSocket.getPort());
}
- public void broadcastMsg(String msg) {
- for (int i = 0; i < listWriter.size(); i++) {
- listWriter.get(i);
+ private void broadcastMsg(String msg) {
+ ListIterator<PrintWriter> iter = listWriter.listIterator();
+ while (iter.hasNext()) {
+ PrintWriter cursorPW = iter.next();
+ cursorPW.println(msg);
+ cursorPW.flush();
while (true) { // le dispatcher est le thread qui execute main()
Socket clientSocket = listenSocket.accept();
System.out.println("Connexion de :" + clientSocket.getInetAddress());
while (true) { // le dispatcher est le thread qui execute main()
Socket clientSocket = listenSocket.accept();
System.out.println("Connexion de :" + clientSocket.getInetAddress());
- Thread serviceThread = new Thread(new EchoServerThreadService(clientSocket));
+ Thread serviceThread = new Thread(new BroadcastThreadService(clientSocket));
serviceThread.start();
}
}
serviceThread.start();
}
}
# NAME = Camilo Juan
CLASSES = \
# NAME = Camilo Juan
CLASSES = \
- EchoServerThreadService.java \
+ BroadcastThreadService.java \