*/
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;
}
*/
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;
}
public void run() {
try {
boolean end = false;
+ //FIXME: Not exiting properly from that loop!
while (!end) {
String rline = client.receiveMsg();
if (rline.equals(".")) {
--- /dev/null
+import java.net.*;\r
+import java.io.*;\r
+import java.util.*;\r
+\r
+public class EchoServerThreadService implements Runnable {\r
+\r
+ private Socket clientSocket;\r
+ private ArrayList<PrintWriter> listWriter;\r
+\r
+ EchoServerThreadService(Socket clientSocket) {\r
+ System.out.println("Creation d'un thread pour repondre a un client, port " + clientSocket.getPort());\r
+ this.clientSocket = clientSocket;\r
+ }\r
+\r
+ public void run() {\r
+ try {\r
+ doService(clientSocket);\r
+ clientSocket.close();\r
+ } catch (IOException e) {\r
+ System.err.println("IOException : " + e);\r
+ e.printStackTrace();\r
+ }\r
+ finally {\r
+ try {\r
+ if (this.clientSocket != null)\r
+ this.clientSocket.close();\r
+ } catch (IOException e) {\r
+ System.err.println("IOException : " + e);\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+ public void doService(Socket clientSocket) throws IOException {\r
+ BufferedReader in;\r
+ PrintStream out;\r
+ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));\r
+ out = new PrintStream(clientSocket.getOutputStream());\r
+ //listWriter.add(new PrintWriter(clientSocket.getOutputStream()));\r
+ boolean end = false;\r
+ while (!end) {\r
+ String theLine = in.readLine();\r
+ if (theLine.equals("."))\r
+ end = true; // le thread de service doit terminer\r
+ out.println(theLine);\r
+ }\r
+ System.out.println("Fin du thread repondant au client, port "\r
+ + clientSocket.getPort());\r
+ }\r
+\r
+ public void broadcastMsg(String msg) {\r
+ for (int i = 0; i < listWriter.size(); i++) {\r
+ listWriter.get(i);\r
+ }\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+import java.net.*;\r
+import java.io.*;\r
+\r
+public class Main {\r
+ public static void main(String[] args) {\r
+ ServerSocket listenSocket = null;\r
+ try {\r
+ listenSocket = new ServerSocket(Integer.parseInt(args[0])); // port\r
+ while (true) { // le dispatcher est le thread qui execute main()\r
+ Socket clientSocket = listenSocket.accept();\r
+ System.out.println("Connexion de :" + clientSocket.getInetAddress());\r
+ Thread serviceThread = new Thread(new EchoServerThreadService(clientSocket));\r
+ serviceThread.start();\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ System.err.println("IOException : " + e);\r
+ e.printStackTrace();\r
+ }\r
+ finally {\r
+ try {\r
+ if (listenSocket != null)\r
+ listenSocket.close();\r
+ } catch (IOException e) {\r
+ System.err.println("IOException : " + e);\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+}\r
--- /dev/null
+# 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 \<return> 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