Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.InetAddress;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.net.UnknownHostException;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8.  
  9. public class Server {
  10. private static int PORT = 4333;
  11. private ServerSocket serverSocket;
  12. ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  13.  
  14. public static void main(String[] args) {
  15. try {
  16. System.out.println("n====================Server Details====================");
  17. System.out.println("Server Machine: "+ InetAddress.getLocalHost().getCanonicalHostName());
  18. System.out.println("Port number: " + PORT);
  19. } catch (UnknownHostException e1) {
  20.  
  21. e1.printStackTrace();
  22. }
  23.  
  24. try {
  25. Server server = new Server();
  26. server.start();
  27.  
  28. } catch (IOException e) {
  29. System.err.println("Error occured:" + e.getMessage());
  30. System.exit(0);
  31. }
  32. }
  33.  
  34. public Server() throws IOException {
  35. serverSocket = new ServerSocket(PORT);
  36. }
  37.  
  38.  
  39. private void start() throws IOException {
  40. System.out.println("----- Started Blocking Server -----");
  41. while (true) {
  42. Socket socket = serverSocket.accept();
  43. HttpHandler connection = new HttpHandler(socket);
  44. executorService.submit(connection);
  45. }
  46. }
  47.  
  48. }
  49.  
  50. import handler.CPUHandler;
  51. import handler.DBHandler;
  52. import handler.IOHandler;
  53. import handler.MemoryHandler;
  54. import parser.GetRequestParser;
  55.  
  56. import java.io.*;
  57. import java.net.Socket;
  58. import java.util.Dictionary;
  59.  
  60. public class HttpHandler implements Runnable {
  61. private Socket socket;
  62.  
  63. public HttpHandler(Socket socket) {
  64. this.socket = socket;
  65. }
  66.  
  67. public void run() {
  68. try {
  69. handleRequest();
  70. } catch (Exception e) {
  71. System.err.println("Error Occured: " + e.getMessage());
  72. try {
  73. socket.close();
  74. System.exit(0);
  75. } catch (IOException e1) {
  76. System.err.println("Error Closing socket Connection.");
  77. System.exit(0);
  78. }
  79. System.err.println("Server is Terminating!");
  80. }
  81. }
  82.  
  83. private void handleRequest() throws Exception {
  84. InputStream input;
  85. OutputStream output;
  86. input = socket.getInputStream();
  87. output = socket.getOutputStream();
  88. serverRequest(input, output);
  89. output.close();
  90. input.close();
  91.  
  92. socket.close();
  93. }
  94.  
  95.  
  96. private void serverRequest(InputStream input, OutputStream output) throws Exception {
  97. String line;
  98. BufferedReader bf = new BufferedReader(new InputStreamReader(input));
  99. while ((line = bf.readLine()) != null) {
  100. if (line.length() <= 0) {
  101. break;
  102. }
  103. if (line.startsWith("GET")) {
  104. // read the request and write the response to output
  105.  
  106. break;
  107. }
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement