Computer networks

User Generated

avgrrfuxhfhzon

Programming

Description

Web Server in Java: Part I

In this assignment, you will develop a simple Web server in two steps- in the first part, you will create a multi-threaded TCP server that is able to receive an HTTP packet from a browser. In the second step, you will implement the GET functionality that allows the server to respond to a browser request. At the end of assignment 5, you will have built a Web server that is capable of processing simple web requests such as an HTML page with embedded images for multiple clients. As you are developing the code, you can test your server from a Web browser. But remember that you are not serving through the standard port 80, so you need to specify the port number within the URL that you give to your browser. For example, if you are testing your program on toolman, your server is listening to port 5910, and you want to retrieve the file index.html, then you would specify the following URL within the browser:

http://toolman.wiu.edu:5910/index.html

If you omit ":5910", the browser will assume port 80 which most likely will not have a server listening on it. To simplify this programming task, you should develop the code in two stages. In the first stage, you will write part of the server that simply displays the contents of the HTTP request message that it receives. After this program is running properly, you will add the code required to generate an appropriate response.

Multi-threaded Server

Threads are useful in network applications where clients may concurrently connect to a server. If the server is single threaded, then that may delay the processing for each connected client. Having a thread that carries out the processing for each connected client speeds up the server processing time experienced by clients. Threads in Java can be created by two ways:

 Implementing the Runnable interface

 Extending the Thread class

Refer to https://docs.oracle.com/javase/tutorial/essential/...

for an example of thread creation in Java.

Application Processing: HTTP GET method

Write a java program named HttpServer.java that opens a TCP socket at a user specified port to listen to HTTP GET requests. This should be relatively easy and similar to the TCP Socket programs that we wrote in class. Create a Socket that listens for incoming client requests at a specified port. When the server receives a request, obtain the input and output streams from the socket. Read the HTTP packet from the client as a set of strings, each of which is terminated by a line feed (\n). Once the packet is read, display the content to the screen.

How would you know that you are done reading the packet?

The last string should have length zero since it only has a carriage return (\r) and line feed (\n) character in it.

Parse the request line in the packet, and print the following information:

Request Type:

URL Requested:

HTTP version:

Header Line1:

Header Line 2: …

Header Line n

Specifications

The web server should be started with the following command

java HttpServer<port number>

where port number is user specified at run time.

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Attached.

Surname 1
Name:
Instructor:
Course:
Date:
Web Server in Java: Part I

The first stage of implementing the Web server will probably be multi-threaded. It is in
this stage where each request is processed as it comes. It is worth noting that each processing of
a request takes place in a separate execution. (Harold 15-25)This makes it possible for the server
to provide service to several clients in parallel, or to simply transfer multiple files to one client in
parallel. During creation of a new execution thread, an instance of some class is passed to the
constructor of the thread. It is this class that is able to implement a Runnable interface. It is for
this very reason as to why a separate class called HttpRequest is defined. The program below
shows the Web server structure:
import java.io.* ;
import java.net.* ;
import java.util.* ;
public final class WebServer
{
public static void main(String argv[]) throws Exception
{
...
}
}
final class HttpRequest implements Runnable
{
...
}

The next step is to add a code so as to be able to analyze the request message from the
client and provide the necessary response. However, before adding the code, the program ought

Surname 2
to be compiled and tested using a browser. Then the lines of code below are added so as to close
the socket and stream connection.
// Close streams and socket.
os.close();
br.close();
socket.close();

The above program is successfully compiled. Specification involves running the program
using the available portal number and testing it using a browser. This is accomplished by adding
the servers’ IP address to the browser address textbox. For example, in this case, the tool man
name is toolman.wiu.edu and the port number is 5910, when we specify the URL:
http://toolman.wiu.edu:5910/, the server is supposed to display the content of the HTTP request
message.

Surname 3
Works Cited
Harold, Elliotte Rusty. Java network programming. Sebastopol, CA: O'Reilly, 2013. Print.
Hughes-Croucher, Tom and Mike Wilson. ...


Anonymous
Really useful study material!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags