From: Jérôme Benoit Date: Mon, 12 Mar 2018 22:04:01 +0000 (+0100) Subject: TD2: Add multithreaded server primary code. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ef258436b31ba460b5038c5b7b62a06ea8968b2d;p=TD_SR.git TD2: Add multithreaded server primary code. Signed-off-by: Jérôme Benoit --- diff --git a/TD2/client/ClientSimplifie.java b/TD2/client/ClientSimplifie.java index 84fecb5..5a2e048 100644 --- a/TD2/client/ClientSimplifie.java +++ b/TD2/client/ClientSimplifie.java @@ -69,7 +69,7 @@ public class ClientSimplifie { */ public String receiveMsg() throws IOException { String line = new String(); - //FIXME: read only the line before the ending newline + //FIXME?: read only the line before the ending newline line = lecture.readLine(); return line; } diff --git a/TD2/client/SocketClient.java b/TD2/client/SocketClient.java index b8e0c0b..8bc5d4f 100644 --- a/TD2/client/SocketClient.java +++ b/TD2/client/SocketClient.java @@ -69,7 +69,7 @@ public class SocketClient { */ public String receiveMsg() throws IOException { String line = new String(); - //FIXME: read only the line before the ending newline + //FIXME?: read only the line before the ending newline line = lecture.readLine(); return line; } diff --git a/TD2/client/ThreadClientReceive.java b/TD2/client/ThreadClientReceive.java index 70f21b6..e3b727b 100644 --- a/TD2/client/ThreadClientReceive.java +++ b/TD2/client/ThreadClientReceive.java @@ -11,6 +11,7 @@ public class ThreadClientReceive implements Runnable { public void run() { try { boolean end = false; + //FIXME: Not exiting properly from that loop! while (!end) { String rline = client.receiveMsg(); if (rline.equals(".")) { diff --git a/TD2/server/EchoServerThreadService.java b/TD2/server/EchoServerThreadService.java new file mode 100644 index 0000000..e04d18f --- /dev/null +++ b/TD2/server/EchoServerThreadService.java @@ -0,0 +1,58 @@ +import java.net.*; +import java.io.*; +import java.util.*; + +public class EchoServerThreadService implements Runnable { + + private Socket clientSocket; + private ArrayList listWriter; + + EchoServerThreadService(Socket clientSocket) { + System.out.println("Creation d'un thread pour repondre a un client, port " + clientSocket.getPort()); + this.clientSocket = clientSocket; + } + + public void run() { + try { + doService(clientSocket); + clientSocket.close(); + } catch (IOException e) { + System.err.println("IOException : " + e); + e.printStackTrace(); + } + finally { + try { + if (this.clientSocket != null) + this.clientSocket.close(); + } catch (IOException e) { + System.err.println("IOException : " + e); + e.printStackTrace(); + } + } + } + + public void doService(Socket clientSocket) throws IOException { + BufferedReader in; + PrintStream out; + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + out = new PrintStream(clientSocket.getOutputStream()); + //listWriter.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 + out.println(theLine); + } + 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); + } + + } + +} diff --git a/TD2/server/Main.java b/TD2/server/Main.java new file mode 100644 index 0000000..c050448 --- /dev/null +++ b/TD2/server/Main.java @@ -0,0 +1,31 @@ +import java.net.*; +import java.io.*; + +public class Main { + public static void main(String[] args) { + ServerSocket listenSocket = null; + try { + listenSocket = new ServerSocket(Integer.parseInt(args[0])); // port + 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)); + serviceThread.start(); + } + } + catch (Exception e) { + System.err.println("IOException : " + e); + e.printStackTrace(); + } + finally { + try { + if (listenSocket != null) + listenSocket.close(); + } catch (IOException e) { + System.err.println("IOException : " + e); + e.printStackTrace(); + } + } + } + +} diff --git a/TD2/server/Makefile b/TD2/server/Makefile new file mode 100644 index 0000000..e3a745a --- /dev/null +++ b/TD2/server/Makefile @@ -0,0 +1,90 @@ +# define compiler and compiler flag variables +# define a variable for compiler flags (JFLAGS) +# define a variable for the compiler (JC) +# define a variable for the Java Virtual Machine (JVM) + +JFLAGS = -g +JC = javac +JVM = java + +# +# Clear any default targets for building .class files from .java files; we +# will provide our own target entry to do this in this makefile. +# make has a set of default targets for different suffixes (like .c.o) +# Currently, clearing the default for .java.class is not necessary since +# make does not have a definition for this target, but later versions of +# make may, so it doesn't hurt to make sure that we clear any default +# definitions for these +# + +.SUFFIXES: .java .class + + +# +# Here is our target entry for creating .class files from .java files +# This is a target entry that uses the suffix rule syntax: +# DSTS: +# rule +# DSTS (Dependency Suffix Target Suffix) +# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency +# file, and 'rule' is the rule for building a target +# '$*' is a built-in macro that gets the basename of the current target +# Remember that there must be a < tab > before the command line ('rule') +# + +.java.class: + $(JC) $(JFLAGS) $*.java + + +# +# CLASSES is a macro consisting of N words (one for each java source file) +# When a single line is too long, use \ to split lines that then will be +# considered as a single line. For example: +# NAME = Camilo \ + Juan +# is understood as +# NAME = Camilo Juan + +CLASSES = \ + EchoServerThreadService.java \ + Main.java + +# +# MAIN is a variable with the name of the file containing the main method +# + +MAIN = Main + +# +# the default make target entry +# for this example it is the target classes + +default: classes + + +# Next line is a target dependency line +# This target entry uses Suffix Replacement within a macro: +# $(macroname:string1=string2) +# In the words in the macro named 'macroname' replace 'string1' with 'string2' +# Below we are replacing the suffix .java of all words in the macro CLASSES +# with the .class suffix +# + +classes: $(CLASSES:.java=.class) + + +# Next two lines contain a target for running the program +# Remember the tab in the second line. +# $(JMV) y $(MAIN) are replaced by their values + +run: $(MAIN).class + $(JVM) $(MAIN) + +# this line is to remove all unneeded files from +# the directory when we are finished executing(saves space) +# and "cleans up" the directory of unneeded .class files +# RM is a predefined macro in make (RM = rm -f) +# + +clean: + $(RM) *.class