You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerApplication.java 3.5KB

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