Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

COMP 2100 Lab 3: Building a Web Server with Socket Programming Labs should be MS Word or PDF format (graphs, screenshots, tables, etc. are highly

COMP 2100 Lab 3: Building a Web Server with Socket Programming

Labs should be MS Word or PDF format (graphs, screenshots, tables, etc. are highly encouraged and should be added to the document to be turned in) A socket is an abstraction through which an application may send and receive data, in much the same way as an open file handle allows an application to read and write data to stable storage. The main types of sockets in TCP/IP today are stream sockets and datagram sockets. Stream sockets use TCP as the end-to-end protocol (with IP underneath). Datagram sockets use UDP (again, with IP underneath) and are a best-effort datagram service (can send individual messages up to 65,500 bytes). Socket, Protocols, and Ports: Basic TCP/IP Model: In this lab, you will learn the basics of socket programming for TCP connections: how to create a socket, bind it to a specific address and port, as well as send and receive a HTTP packet. You will also learn some basics of HTTP header format.

Task 1 (10 pts) You will develop a web server that handles one HTTP request at a time. Here is the web server process

: Create a connection socket when contacted by a client (browser) Python: connectionSocket, addr = serverSocket.accept() Java: Socket connection = socket.accept();

Receive the HTTP request from this connection Python: connectionSocket.recv(1024).decode() Java: // Get a reference to the socket's input and output streams. InputStream is = socket.getInputStream(); DataOutputStream os = new DataOutputStream(socket.getOutputStream()); // Set up input stream filters. BufferedReader br = new BufferedReader(new InputStreamReader(is)); // Get the request line of the HTTP request message. String requestLine = br.readLine();

Parse the HTTP request to determine the specific file being requested Sample HTTP request GET /HelloWorld.html HTTP/1.1 Host: 192.168.86.28:6789 Connection: keep-alive Purpose: prefetch Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9

Get the requested file from the servers file system

Create an HTTP response message consisting of the requested file preceded by header lines Sample header lines: HTTP/1.1 200 OK

Send the response over the TCP connection to the requesting client (browser). If file is not found in server system, then the server should return a 404 Not Found error message Python: connectionSocket.send(message.encode()) Java: DataOutputStream os = new DataOutputStream(socket.getOutputStream()); os.writeBytes(message) Running the Server Put an HTML file (e.g., HelloWorld.html) in the same directory that the server is in. Run the server program. Determine the IP address of the host that is running the server (e.g., 128.238.251.26). From another host, open a browser and provide the corresponding URL. For example: http://128.238.251.26:6789/HelloWorld.html HelloWorld.html is the name of the file you placed in the server directory. Note also the use of the port number after the colon. You need to replace this port number with whatever port you have used in the server code. In the above example, we have used the port number 6789. The browser should then display the contents of HelloWorld.html. If you omit ":6789", the browser will assume port 80 and you will get the web page from the server only if your server is listening at port 80. Then try to get a file that is not present at the server. You should get a 404 Not Found message.

Task 2 (10 pts) Instead of using a browser, write your own HTTP client to test your server. Your client will connect to the server using a TCP connection, send an HTTP request to the server, and display the server response as an output. You can assume that the HTTP request sent is a GET method.

Task 3 (Optional, 10 pts as bonus) Currently, the web server handles only one HTTP request at a time. Implement a multithreaded server that is capable of serving multiple requests simultaneously. Using threading, first create a main thread in which your modified server listens for clients at a fixed port. When it receives a TCP connection request from a client, it will set up the TCP connection through another port and services the client request in a separate thread. There will be a separate TCP connection in a separate thread for each request/response pair.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 3 Lnai 8726

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448440, 978-3662448441

More Books

Students also viewed these Databases questions