Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. public class Server implements Runnable {
  2. public static final int PORTNUMBER = 8540;
  3. public static final int MAX_CLIENTS = 3;
  4. public static boolean shutdownFlag = false;
  5.  
  6. public static void main(String[] args) {
  7. Server s = new Server();
  8. s.run();
  9.  
  10. }
  11.  
  12. @Override
  13. public void run() {
  14. ExecutorService executor = null;
  15. try (ServerSocket serverSocket = new ServerSocket(PORTNUMBER);) {
  16. executor = Executors.newFixedThreadPool(MAX_CLIENTS);
  17. System.out.println("Waiting for clients");
  18. while (true) {
  19. Socket clientSocket = serverSocket.accept();
  20. //clientSocket.setSoTimeout(10000);
  21. Runnable worker = new RequestHandler(clientSocket);
  22. executor.execute(worker);
  23. System.out.println("Only printed if new Client arrives");
  24. if (shutdownFlag) {
  25. System.out.println("Executing shutdown");
  26. executor.shutdown();
  27. serverSocket.close();
  28. }
  29. }
  30. } catch (IOException e) {
  31. System.out
  32. .println("Exception caught when trying to listen on port "
  33. + PORTNUMBER + " or listening for a connection");
  34. System.out.println(e.getMessage());
  35. } finally {
  36. if (executor != null) {
  37. executor.shutdown();
  38. }
  39. }
  40.  
  41. }
  42.  
  43. public class Client {
  44.  
  45. public static final String HOSTNAME = "localhost";
  46. public static final int PORTNUMBER = 8540;
  47. public static void main(String[] args) throws IOException {
  48.  
  49.  
  50.  
  51. try (Socket echoSocket = new Socket(HOSTNAME, PORTNUMBER);
  52. PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
  53. // InputStream test = echoSocket.getInputStream();
  54. BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
  55. BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
  56. String userInput;
  57. while ((userInput = stdIn.readLine()) != null) {
  58. out.println(userInput);
  59. System.out.println("echo: " + in.readLine());
  60. if(userInput.equals("BYE")){
  61. break;
  62. }
  63.  
  64. }
  65. }catch (UnknownHostException e) {
  66. System.err.println("Don't know about host " + HOSTNAME);
  67. System.exit(1);
  68. }catch (IOException e) {
  69. System.err.println("Couldn't get I/O for the connection to " + HOSTNAME);
  70. System.exit(1);
  71. }
  72.  
  73. }
  74. }
  75.  
  76. public class RequestHandler implements Runnable {
  77. //private final String password = "passwort";
  78. private final Socket client;
  79. ServerSocket serverSocket = null;
  80.  
  81. public RequestHandler(Socket client) {
  82. this.client = client;
  83. }
  84.  
  85. @Override
  86. public void run() {
  87. try (BufferedReader in = new BufferedReader(new InputStreamReader(
  88. client.getInputStream()));
  89. BufferedWriter writer = new BufferedWriter(
  90. new OutputStreamWriter(client.getOutputStream()));) {
  91. System.out.println("Thread started with name:"
  92. + Thread.currentThread().getName());
  93. String userInput;
  94. String serverResponse;
  95.  
  96. while ((userInput = in.readLine()) != null) {
  97. serverResponse = processInput(userInput);
  98. System.out.println("Received message from "
  99. + Thread.currentThread().getName() + " : " + userInput);
  100. writer.write("Sever Response : " + serverResponse);
  101. writer.newLine();
  102. writer.flush();
  103. }
  104. } catch (IOException e) {
  105. System.out.println("I/O exception: " + e);
  106. } catch (Exception ex) {
  107. System.out.println("Exception in Thread Run. Exception : " + ex);
  108. }
  109. }
  110.  
  111. public String processInput(String input) {
  112. boolean commandFound = false;
  113. String output = "";
  114. try {
  115. if (input.getBytes("UTF-8").length > 255)
  116. output = "Max string length exceeded";
  117. } catch (UnsupportedEncodingException e) {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. }
  121. Pattern allPattern = Pattern
  122. .compile("(?<lower>^LOWERCASE\s.+)|(?<upper>^UPPERCASE\s.+)|(?<reverse>^REVERSE\s.+)|(?<bye>^BYE)|(?<shutdown>^SHUTDOWN passwort)");
  123.  
  124. Matcher allMatcher = allPattern.matcher(input);
  125. if (allMatcher.find()) {
  126. String lower = allMatcher.group("lower");
  127. String upper = allMatcher.group("upper");
  128. String reverse = allMatcher.group("reverse");
  129. String bye = allMatcher.group("bye");
  130. String shutdown = allMatcher.group("shutdown");
  131. commandFound = true;
  132. if (lower != null) {
  133. output = lower.substring(10).toLowerCase();
  134. }
  135. if (upper != null) {
  136. output = upper.substring(10).toUpperCase();
  137. }
  138. if (reverse != null) {
  139. output = new StringBuilder(reverse.substring(8)).reverse()
  140. .toString();
  141. }
  142. if (bye != null) {
  143. output = "BYE";
  144. }
  145. if (shutdown != null) {
  146. output = "SHUTDOWN";
  147. Server.shutdownFlag = true;
  148. }
  149. } else {
  150. commandFound = false;
  151. output = "UNKNOWN COMMAND";
  152. }
  153.  
  154.  
  155. if (commandFound) {
  156. output = "OK ".concat(output);
  157. } else {
  158. output = "ERROR ".concat(output);
  159.  
  160. }
  161. return output;
  162.  
  163. }
  164. }
  165.  
  166. class Server {
  167. private boolean shutdownFlag = false; // This can't be static anymore.
  168. public static final Server SERVER = new Server();
  169. public static void main(String[] args) {
  170. SERVER.run();
  171. }
  172. private void run() {
  173. // Here goes everything that used to be inside main...
  174. // Now you have the Server.SERVER instance to use outside the class
  175. // to shut things down or whatever ...
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement