Advanced Internet Technologies

Advanced Internet Technologies - Web Server Programming...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Web Server Programming Assignment (optional) We are going to implement version 1.0 of HTTP, as defined in RFC 1945 , where HTTP requests are sent for each component of the Web page. The server will be able to listen to a fixed port. When it receives a TCP connection request, it sets up a TCP connection through the port and services the request. To simplify this programming task, we will develop the code in two stages. In the first stage, you will write a web 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 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 your machine's name is ee.cityu.edu.hk , your server is listening to port 6789, and you want to retrieve the file index.html , then you would specify the following URL within the browser: http://ee.cityu.edu.hk:6789/index.html If you omit ":6789", the browser will assume port 80 which most likely will not have a server listening on it. 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. Web Server in Java: In the following steps, we will go through the code for the first implementation of our Web Server. WebServer.java  import java.io.* ; import java.net.* ; import java.util.* ; public final class WebServer { Normally, Web servers process service requests that they receive through well-known port number 80. You can choose any port higher than 1024, but remember to use the same port number when making requests to your Web server from your browser.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
public static void main(String argv[]) throws Exception { // Set the port number. int port = 6789; Next, we open a socket and wait for a TCP connection request. Because we will be servicing request messages indefinitely, we place the listen operation inside of an infinite loop. This means we will have to terminate the Web server by pressing ^C on the keyboard. // Establish the listen socket. ServerSocket socket = new ServerSocket(port); // Process HTTP service requests in an infinite loop. while (true) {     // Listen for a TCP connection request.     Socket connection = socket.accept(); When a connection request is received, we create an HttpRequest object, passing to its constructor a reference to the Socket object that represents our established connection with the client.     // Construct an object to process the HTTP request 
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/11/2011 for the course EE 5413 taught by Professor Hingcheungso during the Spring '10 term at City University of Hong Kong.

Page1 / 10

Advanced Internet Technologies - Web Server Programming...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online