In this assignment you will learn how to use sockets in Java and how web servers work. In the first part of the assignment, you will write two programs: a simple client and a simple server that communicate over a TCP connection. In the second part, you will build a Web server capable of successfully transmitting your home page to a Web browser.
It is assumed that you know how to modify the HTML files stored in your home directory. If you are not familiar with the Java programming language, you may want to consult documentation of the Java API.
Evaluation of your work will be based on the demonstrated operation of these programming tasks. Also, any extra functionality that you incorporate into your Web server, which is beyond the assigned steps in this lab, will be considered in determining a grade for your performance on this lab.
For part 1, you will write a TCP client and server in the Java language. When the server is run, it establishes a socket on which it listens for a TCP connection request. When the client is run, it sends a TCP connection request to the server. The server accepts the connection request, and waits to receive a message. The client sends a text string for a message, and then waits to receive a response from the server. The server responds with a text string and then closes the socket connection.
When the server executes, it establishes a socket and listens for a TCP connection request. When it receives a connection request, it sends a text string and then terminates. Both the port number and the text string message are specified on the command line.
You are free to implement this any way you like, but if you are new to the Java language, you should follow the steps for developing the simple server.
When the client runs, it establishes a socket and sends a connection request message to the IP address of a running server. After connecting to the server, it reads a stream of text data from the socket until it reaches a new line character. It prints the text and terminates. Both the server's port number and IP address are specified on the command line.
Once again, you are free to implement this program the way you like, but if you are new to the Java language, you should follow the steps for developing the simple client.
In this part of the lab we will develop a Web server in two steps. In the end, you will have built a multi-threaded Web server that is capable of processing multiple simultaneous service requests in parallel. Your Web server should be capable of delivering your home page (or any other HTML file) to your browser.
Normally, Web servers process service requests that they receive through well-known port number 80. However, using port numbers below 1024 typically requires administrator or root privileges, so we will have to run our Web servers on some other port. As in our simple TCP server, we will obtain this port number from the command line.
Some modern web servers use HTTP version 1.1, and some very old servers use version 0.9. We are going to implement version 1.0 of HTTP, as defined in RFC 1945, where separate HTTP requests are sent for each component that comprises a single Web page.
Like the server in part 1, this HTTP server will be listening on some port for a TCP connection request. After a connection request is received, and a new TCP socket is created automatically for communication with the client, and the server reads the stream of text data being sent to it and determines what action to take.
The server will be able to handle multiple simultaneous service requests in parallel. This means that the Web server is multi-threaded. In the main thread, the server listens to a fixed port. When it receives a TCP connection request, it sets up a TCP connection through another port and services the request in a separate thread.
To simplify this programming task, we will develop the code in two stages. In the first stage, you will write a multi-threaded 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.
As you are developing the code, you can test your server from your 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. You can use the loopback IP address 127.0.0.1 to refer to your machine, in case your computer is not registered in DNS (computers in pool rooms typically have names in DNS). For example, if your server is listening on port 8080, and you want to retrieve the file index.html, then you should use the following URL:
http://127.0.0.1:8080/index.html
When the server encounters an error, it sends a response message with the appropriate HTML source so that the error information is displayed in the browser window.
Here are links to the two program development phases.
To improve your web server implement the following improvements:
The following exercises are optional and you can complete them for extra credit.
<FORM METHOD="POST" ACTION="http://127.0.0.1:8080/"> <INPUT name="clienttext" type="text" length="30"> <INPUT type="submit" value="Send Text"> </FORM>