Question: ****In Eclipse-Java Please do just post previous responses. I had to delete part of the material but can be found here: Full File: https://drive.google.com/open?id=1Ap4HMIrj2w48WW3Ij_Iq0qgE_GMp8vwX Write

****In Eclipse-Java Please do just post previous responses. I had to delete part of the material but can be found here:

Full File: https://drive.google.com/open?id=1Ap4HMIrj2w48WW3Ij_Iq0qgE_GMp8vwX

Write a web server that is capable of sending files to a web browser on request. The web server must create a listening socket and accept connections. It must implement just enough of the HTTP/1.1 protocol to enable it to read requests for files and to send the requested files. It should also be able to send error responses to the client when appropriate.

Seeing HTTP in Action

It would be useful to see the HTTP protocol in action before beginning your work. Your program will have to read HTTP requests and send HTTP responses To see what an HTTP request might look like, you can run the program ReadRequest, which can be found in the code directory. To run the program, cd to that directory on the command line, and enter the command java ReadRequest. The program is ready to accept requests from web browsers on port 50505. (It will continue to do so until you terminate the program.) When it receives a request, it simply prints out the request, including all headers. It then closes the connection; it does not send any response back to the browser. To see a request, open a web browser, and enter the URL http://localhost:50505/path/file.txt into the browser's location box. The request will be output to the console window where you are running the ReadRequest program. The first line of the request should be GET /path/file.txt HTTP/1.1". This will be followed by several headers. To send an HTTP request and see the response, you can use the standard telnet program. Type the following lines carefully in a console window: telnet google.com 80 Add a blank line, by pressing return twice after typing the last line. This sends a legal HTTP request for the file index.html. The web server on math.hws.edu will respond by sending a status line followed by some headers and a blank line, followed by the contents of the file. You can try to get some error responses, if you want, such as by asking for a non-existent file instead of index.html or by using a different method instead of GET. Start a new Eclipse project and create your main program class. The basic programming for a server is pretty standard: It just has to create a ServerSocket and use it to accept connection requests. For the main routine in your program, you can just use the main routine from ReadRequest.java. You can copy-and-paste it from here:

public static void main(String[] args) { ServerSocket serverSocket; try { serverSocket = new ServerSocket(LISTENING_PORT); } catch (Exception e) { System.out.println("Failed to create listening socket."); return; } System.out.println("Listening on port " + LISTENING_PORT); try { while (true) { Socket connection = serverSocket.accept(); System.out.println(" Connection from " + connection.getRemoteSocketAddress()); handleConnection(connection); } } catch (Exception e) { System.out.println("Server socket shut down unexpectedly!"); System.out.println("Error: " + e); System.out.println("Exiting."); } } The problem is to write the handleConnection() method. This method gets an already connected socket as a parameter. It can use that socket to get an InputStream and an OutputStream for communicating over the connection. It can read the request from the input stream and send a response on the output stream. Finally, it can close the connection. You can use some ideas (and maybe some code) from the handleConnection method in ReadRequest.java, but the method that you are writing will be a good deal more complicated. It is very important that:

(a) the handleConnection method should catch and handle any exception that occurs, so that the exception does not crash the whole sever, and (b) the socket must be closed at the end of the method. Use a try..catch..finally statement to make sure that (a) and (b) are done correctly. See ReadRequest.java for an example.

Your program should be ready to send error responses to the client, as well as fulfilling legitimate requests. The next section asks you to implement error-handling. For now, you can simply return from the handleConnection method when you detect an error. The first three tokens that you read from the input stream should be "GET", the path to the file that is being requested, and "HTTP/1.1" (or, just possibly, "HTTP/1.0"). If you can't read three tokens, or if they are not of the expected form, you should consider that to be an error. String rootDirectory = "/home/mcorliss/www" Assuming that rootDirectory is the root directory of your server and pathToFile is the path to the file as given in the request from the browser, then the full name of the file is rootDirectory + pathToFile, and you can create a File object to represent the file that is being requested as follows: File file = new File(rootDirectory + pathToFile); Once you have found the file and know that it is a regular file and that you can read it, you are ready to send a response to the browser. (If the file is a directory, you can't send it, but a typical server in this case will send the contents of a file named index.html in that directory, if it exists. You can think about how to implement this if you want.) Before you send the file itself, you have to send the status line, some headers, and an empty line. You can use a PrintWriter to do this. However, the HTTP protocol specifies that ends-of-line should be indicated by " " rather than the " " that is standard in Linux. Although I have found that it doesn't matter when sending text documents, it does seem to matter when sending images. So, instead of using out.println(x), you should use out.print(x + " ") to send a line of text, and use out.print(" ") to send a blank line. The status line to indicate a good response should be:

HTTP/1.1 200 OK (with " " at the end). You should send three headers: "Connection", "Content-Length", and "Content-Type". For the Connection header, you can send Connection: close which informs the browser that you are going to close the connection after sending the file. The Content-Length header should specify the number of bytes in the file, which you can find with the file.length() method. The Content-Type header tells the browser what kind of data is in the file. It can generally be determined from the extension part of the file name. Here is a method that will return the proper content type for many kinds of files: private static String getMimeType(String fileName) { int pos = fileName.lastIndexOf('.'); if (pos < 0) // no file extension in name return "x-application/x-unknown"; String ext = fileName.substring(pos+1).toLowerCase(); if (ext.equals("txt")) return "text/plain"; else if (ext.equals("html")) return "text/html"; else if (ext.equals("htm")) return "text/html"; else if (ext.equals("css")) return "text/css"; else if (ext.equals("js")) return "text/javascript"; else if (ext.equals("java")) return "text/x-java"; else if (ext.equals("jpeg")) return "image/jpeg"; else if (ext.equals("jpg")) return "image/jpeg"; else if (ext.equals("png")) return "image/png"; else if (ext.equals("gif")) return "image/gif"; else if (ext.equals("ico")) return "image/x-icon"; else if (ext.equals("class")) return "application/java-vm"; else if (ext.equals("jar")) return "application/java-archive"; else if (ext.equals("zip")) return "application/zip"; else if (ext.equals("xml")) return "application/xml"; else if (ext.equals("xhtml")) return"application/xhtml+xml"; else return "x-application/x-unknown"; // Note: x-application/x-unknown is something made up; // it will probably make the browser offer to save the file. } with a blank line at the end. And don't forget to flush the PrintWriter after sending this data. Finally, it's time to send the data from the file itself! The file is not necessarily text, and in any case it should be sent as a stream of bytes. The following method can be used to copy the content of the file to the socket's output stream: private static void sendFile(File file, OutputStream socketOut) throws IOException { InputStream in = new BufferedInputStream(new FileInputStream(file)); OutputStream out = new BufferedOutputStream(socketOut); while (true) { int x = in.read(); // read one byte from file if (x < 0) break; // end of file reached out.write(x); // write the byte to the socket } out.flush(); }

You should have a minimal but functional multi-threaded web server.

Required File: https://drive.google.com/open?id=1WuHBCmNFN0wIO9KNY3HAWPkXKjjWWUcP

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!