瀏覽代碼

server client read command

develop
Robin Thoni 8 年之前
父節點
當前提交
0082d198da

+ 1
- 1
client/src/main/java/com/uqac/rthoni/com/java_rmi/client/ClientApplication.java 查看文件

@@ -7,6 +7,6 @@ public class ClientApplication {
7 7
 
8 8
     public static void run(String host, int port, String inputFile, String outputFile)
9 9
     {
10
-        System.out.println(String.format("Connecting to %1s:%2d...", host, port));
10
+        System.out.println(String.format("Connecting to %s:%d...", host, port));
11 11
     }
12 12
 }

+ 93
- 2
server/src/main/java/com/uqac/rthoni/com/java_rmi/server/ServerApplication.java 查看文件

@@ -1,13 +1,104 @@
1 1
 package com.uqac.rthoni.com.java_rmi.server;
2 2
 
3
+import com.uqac.rthoni.java_rmi.common.Command;
4
+
5
+import java.io.BufferedReader;
6
+import java.io.IOException;
7
+import java.io.InputStreamReader;
8
+import java.net.InetAddress;
9
+import java.net.ServerSocket;
10
+import java.net.Socket;
11
+
3 12
 /**
4 13
  * Created by robin on 9/15/16.
5 14
  */
6 15
 public class ServerApplication {
7 16
 
17
+    public static String ipToString(InetAddress ip) {
18
+        String ipString = ip.toString();
19
+        if (!ipString.isEmpty() && ipString.startsWith("/")) {
20
+            ipString = ipString.substring(1);
21
+        }
22
+        return ipString;
23
+    }
24
+
25
+    public static String getFullIp(InetAddress ip) {
26
+        try {
27
+            InetAddress address = InetAddress.getByName(ip.toString().split("/")[1]);
28
+            return String.format("%s/%s", address.getCanonicalHostName(), ipToString(ip));
29
+        }
30
+        catch (Exception e) {
31
+            return ipToString(ip);
32
+        }
33
+    }
34
+
8 35
     public static void run(int port, String sourceDir, String classDir, String logFile)
9 36
     {
10
-        String ip = "0.0.0.0";
11
-        System.out.println(String.format("Listening for clients on %1s:%2d...", ip, port));
37
+        String host = "0.0.0.0";
38
+        InetAddress ip = null;
39
+        try {
40
+            ip = InetAddress.getByName(host);
41
+        }
42
+        catch (Exception e) {
43
+        }
44
+        if (ip == null) {
45
+            System.err.format("Failed to resolve '%s'\n", host);
46
+            System.exit(1);
47
+        }
48
+        String ipString = ipToString(ip);
49
+
50
+        try {
51
+            ServerSocket serverSocket = new ServerSocket(port, 1, ip);
52
+            System.out.format("Listening for clients on %s:%d...\n", ipString, port);
53
+            runServer(serverSocket);
54
+        } catch (IOException e) {
55
+            System.err.format("Failed to listen on %s:%d: %s\n", ipString, port, e.getMessage());
56
+        }
57
+    }
58
+
59
+    public static void runServer(ServerSocket serverSocket)
60
+    {
61
+        boolean stop = false;
62
+        while (!stop) {
63
+            Socket client;
64
+            try {
65
+                client = serverSocket.accept();
66
+            }
67
+            catch (Exception e) {
68
+                System.err.format("Failed to accept client: %s\n", e.getMessage());
69
+                break;
70
+            }
71
+            String ipString = getFullIp(client.getInetAddress());
72
+            System.out.format("New client: %s:%d\n", ipString, client.getPort());
73
+            try {
74
+                handleClient(client);
75
+            } catch (Exception e) {
76
+                System.out.format("Error when handling client: %s\n", e.getMessage());
77
+            }
78
+            try {
79
+                client.close();
80
+            } catch (Exception e) {
81
+            }
82
+            System.out.format("Client disconnected: %s:%d\n", ipString, client.getPort());
83
+        }
84
+    }
85
+
86
+    private static void handleClient(Socket client)
87
+    {
88
+        String str;
89
+        try {
90
+            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
91
+            str = in.readLine();
92
+        } catch (IOException e) {
93
+            System.err.format("Failed to read from client: %s", e.getMessage());
94
+            return;
95
+        }
96
+        Command command = Command.fromString(str);
97
+        if (command == null) {
98
+            System.err.format("Failed to deserialize command: %s\n", str);
99
+        }
100
+        else {
101
+            System.out.format("Received command: %s\n", command.getCommandName());
102
+        }
12 103
     }
13 104
 }

+ 4
- 4
src/main/java/com/uqac/rthoni/java_rmi/Main.java 查看文件

@@ -14,20 +14,20 @@ public class Main {
14 14
         PrintStream printStream = (error ? System.err : System.out);
15 15
 
16 16
         printStream.println("Usage:");
17
-        printStream.println(String.format("%1s server port source_dir class_dir log_file", name));
17
+        printStream.format("%s server port source_dir class_dir log_file\n", name);
18 18
         printStream.println("\tPut the program in server mode");
19 19
         printStream.println("\tport: the port to listen on [1-65535]");
20 20
         printStream.println("\tsource_dir: the folder to search for java files");
21 21
         printStream.println("\tclass_dir: the folder to build java files");
22 22
         printStream.println("\tlog_file: the file to log events");
23
-        printStream.println(String.format("%1s client hostname port input_file output_file", name));
23
+        printStream.format("%s client hostname port input_file output_file\n", name);
24 24
         printStream.println("\tPut the program in client mode");
25 25
         printStream.println("\thost: the remote server ip or hostname");
26 26
         printStream.println("\tport: the port to connect on [1-65535]");
27 27
         printStream.println("\tinput_file: the command file to use");
28 28
         printStream.println("\toutput_file: the file to output command results");
29
-        printStream.println(String.format("%1s --help", name));
30
-        printStream.println(String.format("%1s -h", name));
29
+        printStream.format("%s --help\n", name);
30
+        printStream.format("%s -h\n", name);
31 31
         printStream.println("\tPrint this help and exit");
32 32
 
33 33
         if (error) {

Loading…
取消
儲存