Posted By

shaylenpatel7 on 04/10/15


Tagged

server web networking


Versions (?)

Web Server


 / Published in: Java
 

Multi-threaded web server

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

Report this snippet  

You need to login to post a comment.