Developing a multithreaded web server that is capable of serving multiple reques
ID: 3897657 • Letter: D
Question
Developing a multithreaded web server that is capable of serving multiple requests in parallel
2 Laboratory Assignment 1 A Problem Develop in Java a multithreaded Web server that is capable of serving multiple requests in parallel. Implement version 1.0 of HTTP as defined in RFC 1945. B Specifications 1 To simplify the programming task develop your code in two stages. In the first stage e a multithreaded server that simply displays the contents of the HTTP message that it receives. In the second stage you will add the code required to you will writ request generate an appropriate response 2 Test your server with a web browser or the simple web browser in Section 8. But since your Web browser will not be serving through the standard port 80 you will need to specify the port number within the URL that you give your browser. When your browser encounters an error, it should send a response message with the appropriate HTML source so that the error information is displayed in the browser window 3Explanation / Answer
Implemented Source Code:-
-----------------------------
package com.challenges;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
public final class WebServer
{
public static void main(String argv[]) throws Exception
{
int port =8888;
ServerSocket WebSocket = new ServerSocket(port);
while(true)
{
Socket connectionSocket = WebSocket.accept();
//Construct object to process HTTP request message
HttpRequest request = new HttpRequest(connectionSocket);
Thread thread = new Thread(request);
thread.start(); //start thread
}
//System.out.println("Successfully Terminated");
//WebSocket.close();
}
}
final class HttpRequest implements Runnable
{
final static String CRLF = " ";
Socket connectionSocket;
public HttpRequest(Socket connectionSocket) throws Exception
{
this.connectionSocket = connectionSocket;
}
public void run()
{
try
{
processRequest();
}
catch(Exception e)
{
System.out.println(e);
}
}
private void processRequest() throws Exception
{
InputStream is = connectionSocket.getInputStream();
DataOutputStream os = new DataOutputStream(connectionSocket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String requestLine = br.readLine();
System.out.println();
System.out.println(requestLine);
InetAddress incomingAddress = connectionSocket.getInetAddress(); // IP address of the incoming connection.
String ipString= incomingAddress.getHostAddress();
System.out.println("The incoming address is: " + ipString);
StringTokenizer tokens = new StringTokenizer(requestLine);
tokens.nextToken();
String fileName = tokens.nextToken();
fileName = "." + fileName;
String headerLine = null;
while ((headerLine = br.readLine()).length() != 0)
{
System.out.println(headerLine);
}
FileInputStream fis = null;
boolean fileExists = true;
try
{
fis = new FileInputStream(fileName);
}
catch (FileNotFoundException e)
{
fileExists = false;
}
//Construct the response message
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
if (fileExists)
{
statusLine = "HTTP/1.1 200 OK: ";
contentTypeLine = "Content-Type: " +
contentType(fileName) + CRLF;
}
else
{
statusLine = "HTTP/1.1 404 Not Found: ";
contentTypeLine = "Content-Type: text/html" + CRLF;
entityBody = "<HTML> <HEAD><TITLE>Not Found</TITLE></HEAD> <BODY>Not Found on Kaitec's Multithreaded WebServer</BODY></HTML>";
}
// Send the status line.
os.writeBytes(statusLine);
// Send the content type line.
os.writeBytes(contentTypeLine);
os.writeBytes(CRLF);
// Send the entity body.
if (fileExists)
{
sendBytes(fis, os);
fis.close();
}
else
{
os.writeBytes(entityBody);
}
os.close(); //Close streams and socket.
br.close();
connectionSocket.close();
}
private static void sendBytes(FileInputStream fis, OutputStream os)throws Exception
{
byte[] buffer = new byte[1024];
int bytes = 0;
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(".jpg"))
return "text/jpg";
if(fileName.endsWith(".gif"))
return "text/gif";
return "application/octet-stream";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.