multithread Webserver


SUBMITTED BY: knightley

DATE: Sept. 3, 2015, 9:29 a.m.

FORMAT: Java

SIZE: 4.5 kB

HITS: 488

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. public final class WebServer {
  5. public static void main(String[] args) throws Exception
  6. {
  7. // Establish the listen socket.
  8. ServerSocket wS = new ServerSocket(6789);
  9. while (true) {
  10. // Listen for a TCP connection request.
  11. Socket cS = wS.accept();
  12. // Construct an object to process the HTTP request message.
  13. HttpRequest request = new HttpRequest(cS);
  14. // Create a new thread to process the request.
  15. Thread thread = new Thread(request);
  16. // Start the thread.
  17. thread.start();
  18. }
  19. }
  20. }
  21. import java.io.*;
  22. import java.net.*;
  23. import java.util.*;
  24. final class HttpRequest implements Runnable {
  25. final static String CRLF = "\r\n";
  26. Socket socket;
  27. // Constructor
  28. public HttpRequest(Socket socket) throws Exception {
  29. this.socket = socket;
  30. }
  31. public void run() {
  32. try {
  33. processRequest();
  34. } catch (Exception e) {
  35. System.out.println(e);
  36. }
  37. }
  38. private void processRequest() throws Exception
  39. {
  40. InputStream is = socket.getInputStream();
  41. DataOutputStream os = new DataOutputStream(socket.getOutputStream());
  42. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  43. String requestLine = br.readLine();
  44. // Display the request line.
  45. System.out.println();
  46. System.out.println(requestLine);
  47. String headerLine = null;
  48. while ((headerLine = br.readLine()).length()!=0) {
  49. System.out.println(headerLine);
  50. }
  51. // Extract the filename from the request line.
  52. StringTokenizer tokens = new StringTokenizer(requestLine);
  53. tokens.nextToken();
  54. tokens.nextToken(); // skip over the method, which should be �GET�
  55. String fileName = tokens.nextToken();
  56. // Prepend a �.� so that file request is within the current directory.
  57. fileName = "." + fileName;
  58. System.out.println("Filename to Get"+fileName);
  59. // Open the requested file.
  60. FileInputStream fis = null;
  61. boolean fileExists = true;
  62. try {
  63. fis = new FileInputStream("webtext.html");
  64. } catch (FileNotFoundException e) {
  65. fileExists = false;
  66. }
  67. // Construct the response message.
  68. String statusLine = null;
  69. String contentTypeLine = null;
  70. String entityBody = null;
  71. if (fileExists) {
  72. statusLine = "200 OK:";
  73. contentTypeLine = "Content-Type: " +
  74. contentType( fileName ) + CRLF;
  75. } else {
  76. statusLine = "HTTP/1.1 404 Not Found:";
  77. contentTypeLine = "Content-Type: text/html"+ CRLF;
  78. entityBody = "" +
  79. "Not Found" +
  80. "Not Found";
  81. }
  82. // Send the status line.
  83. os.writeBytes(statusLine);
  84. // Send the content type line.
  85. os.writeBytes(contentTypeLine);
  86. // Send a blank line to indicate the end of the header lines.
  87. os.writeBytes(CRLF);
  88. // Send the entity body.
  89. if (fileExists) {
  90. sendBytes(fis, os);
  91. fis.close();
  92. } else {
  93. os.writeBytes(entityBody);
  94. }
  95. os.writeBytes(entityBody);
  96. DataOutputStream outToClient = new
  97. DataOutputStream(socket.getOutputStream());
  98. outToClient.writeBytes("HTTP/1.1 200 OK");
  99. outToClient.writeBytes("Content-Length: 100");
  100. outToClient.writeBytes("Content-Type: text/html\n\n");
  101. outToClient.writeBytes(" My First Heading My first paragraph.\n");
  102. outToClient.close();
  103. // Close streams and socket.
  104. os.close();
  105. br.close();
  106. socket.close();
  107. }
  108. private static String contentType(String fileName)
  109. {
  110. if(fileName.endsWith(".html") || fileName.endsWith(".html")) {
  111. return "text/html";
  112. }
  113. return "application/octet-stream";
  114. }
  115. private static void sendBytes(FileInputStream fis, OutputStream os)
  116. throws Exception {
  117. // Construct a 1K buffer to hold bytes on their way to the socket.
  118. byte[] buffer = new byte[1024];
  119. int bytes = 0;
  120. // Copy requested file into the socket�s output stream.
  121. while ((bytes = fis.read(buffer)) != -1) {
  122. os.write(buffer, 0, bytes);
  123. }
  124. }
  125. }

comments powered by Disqus