Question
Multi-Threaded Web Server I need to write a multi-threaded web server to serve incoming requests from clients in a computer network. The web server will
Multi-Threaded Web Server
I need to write a multi-threaded web server to serve incoming requests from clients in a computer network. The web server will be implemented using two Java source files and one directory/folder which contains the website files. The two source files are: - File No. 1: WebRequest.java - File No. 2: WebServer.java
File No. 1 is already implemented but File No. 2 is not implemented and should be implemented. we have a simple website which has only one html file that must show the first and last name. When displayed by a web browser , shown below (index.html)
Test Web Server
Network Programming
first Name:
last name: | I am the mutli-threaded web server that you created |
Assuming an error-prone network, the following points must be done:
1. Study the source code written in File No. 1 carefully.
2. Modify the given html file to display your name and registration number.
3. Write a source code for a multi-threaded web server that utilises the source code in File No. 1. This code (i.e., the multi-threaded web server code) should take two arguments from the user: a) Port number. b) Directory/Folder path and name.
4. Save the source code for the multi-threaded web server in File No. 2.
5. Compile File No. 1 and File No. 2 then run File No. 2
6. Access the web server from a web browser and observe the web page.
7. Monitor the transport-layer states and events using netstat when you access the web server using more than one tab in your web browser. What happens when there is more than one client request? What states are displayed on console/terminal by netstat? Comment on that.
please, let it be well commented.
file No. 1 is written below:
import java.io.* ; import java.net.* ; import java.util.* ;
final class WebRequest implements Runnable { final static String CRLF = " "; Socket socket; String dirName; // Constructor public WebRequest(Socket socket, String dirName) throws Exception { this.socket = socket; this.dirName = dirName; } // Implement the run() method of the Runnable interface. public void run() { try { processRequest(); } catch (Exception e) { System.out.println(e); } }
private void processRequest() throws Exception { // Get a reference to the socket's input and output streams. InputStream is = socket.getInputStream(); DataOutputStream os = new DataOutputStream(socket.getOutputStream()); // Set up input stream filters. BufferedReader br = new BufferedReader(new InputStreamReader(is)); // Get the request line of the HTTP request message. String requestLine = br.readLine();
// Extract the filename from the request line. StringTokenizer tokens = new StringTokenizer(requestLine); tokens.nextToken(); // skip over the method, which should be "GET" String fileName = tokens.nextToken(); // Prepend a "." so that file request is within the current directory. //fileName = "." + fileName ; // Prepend directory path fileName = this.dirName + fileName;
System.out.println("Filename: " + fileName); // Open the requested file. FileInputStream fis = null ; boolean fileExists = true ; try { fis = new FileInputStream(fileName); } catch (FileNotFoundException e) { fileExists = false ; }
// Debug info for private use System.out.println("Incoming!!!"); System.out.println(requestLine); String headerLine = null; while ((headerLine = br.readLine()).length() != 0) { System.out.println(headerLine); } // Construct the response message. String statusLine = null; String contentTypeLine = null; String entityBody = null; if (fileExists) { statusLine = "HTTP/1.0 200 OK" + CRLF; contentTypeLine = "Content-Type: " + contentType(fileName) + CRLF; } else { statusLine = "HTTP/1.0 404 Not Found" + CRLF; contentTypeLine = "Content-Type: text/html" + CRLF; entityBody = "" + "
// Send the content type line. os.writeBytes(contentTypeLine);
// Send a blank line to indicate the end of the header lines. os.writeBytes(CRLF);
// Send the entity body. if (fileExists) { sendBytes(fis, os); fis.close(); } else { os.writeBytes(entityBody) ; }
// Close streams and socket. os.close(); br.close(); socket.close(); }
private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception { // Construct a 1K buffer to hold bytes on their way to the socket. byte[] buffer = new byte[1024]; int bytes = 0; // Copy requested file into the socket's output stream. while ((bytes = fis.read(buffer)) != -1) { os.write(buffer, 0, bytes); } }
private static String contentType(String fileName) { if (fileName.endsWith(".htm") || fileName.endsWith(".html")) { return "text/html"; } if (fileName.endsWith(".ram") || fileName.endsWith(".ra")) { return "audio/x-pn-realaudio"; } return "application/octet-stream" ; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started