Download presentation
Presentation is loading. Please wait.
1
Project 1 Professor Breecher
Computer Networks Project 1 Professor Breecher Networks - Project 1
2
What You Will Do In This Project.
The purpose of this project is to build a simple Web Server and Web Client. This project is to be developed in several stages. It must be written in either C or C++. You have two tasks before you: Develop a multithreaded server that can display the contents of the http GET message. You will use a standard browser to generate this message. Build a client (browser), complete the server, and be able to download .html and .jpg files. An overview of all this is shown on the next slide. Networks - Project 1
3
What You Will Do In This Project.
Client Server Target File Result Firefox or other browser Your multithreaded server prints out client requests None actually accessed. Server prints out text delivered from client Checkpoint 1 Your browser Your server that prints out client requests Server prints out same text as came from commercial browser. Checkpoint 2 Your multithreaded server that delivers files to the client Gettysburg.html Your browser prints out the text from Gettysburg.html. Your server prints out client text and target filename. Checkpoint 3 The Apache server in the CS Department cs.clarku.edu/~jbreecher Your browser prints out the text that Apache sends it. Checkpoint 4 Commercial browser MyJpg.jpg The jpg is visible on the browser. Final Demonstration Networks - Project 1
4
What You Will Do In This Project.
For Each Checkpoint Submission: There is “show and tell” in the lab each week. It’s expected that you can demonstrate the features expected for that checkpoint, and answer questions on how it works. For the Final Submission: For the final submission you need to provide the following: All the code for the web client and the web server (.c and .h files) A makefile that compiles the client and the server code (There’s plenty of on-line information on creating a makefile. A ReadMe (txt) file that spells out exactly how to compile and run the client and the server The file Gettysburg.html. The file MyJpg.jpg that you have created. Combine (via zip or tar) everything up into a single archive file named "your-clark-username_project1.zip". Submit your document by mail electronically by 12:01AM on the day the assignment is due. Networks - Project 1
5
What You Will Do In This Project.
Grading For Project 1: Grade breakdown (%) assuming all documents are present: Each Checkpoint Submitted, compiles and executes = 10% Your Web client displays Gettysburg.html generated by your server = 25% Your Web client displays an .html generated by apache = 25% A commercial browser displays the .jpg served up by your server = 25% Makefile = 5% Successful Show and Tells for me = 15% The grade will be a ZERO if: The code (client or server) does not compile or gives a run-time error To get the FULL CREDIT for the Web client and Web server: The client should obtain the entire html file from the server it is talking to, when its HTTP request succeeds. And your client/server should act appropriately if a non-existent file is requested. Your server should be multi-threaded and able to handle multiple simultaneous requests. Networks - Project 1
6
What You Will Do In This Project.
Specifications – These are expanded in the remaining slides The HTTP Server Execution: Your server should start first (i.e., before the client) and take a command line argument specifying a port number. For example, ./myServer <port-number> The HTTP Client Execution: Your client should take command line arguments and then use the GET method to download a web-page. The arguments are as follows: • The server URL ( or an IP address ( ) • The port number on which to contact the server The path to the target file For example: ./myClient <server-url> <port-number> <Path-To-Target-File> Networks - Project 1
7
What You Will Do In This Project.
Useful Notes: You must chose a server port number larger that 1023 (to be safe, choose a port number larger than 5000). When you connect to cs.clarku.edu, you may get hostnames other than what you expect(as given in the examples above). Please use the linux command hostname to find out where you are connected. I would strongly suggest that you follow the order given in the “What You Will Do” Table. That’s what the checkpoints expect. In writing your code, make sure to check for an error return from your system calls or method invocations, and display an appropriate message. In C this means checking and handling error return codes from your system calls. If you abort your program, the socket may still hang around and the next time you try to bind a new socket to the port ID you previously used (but never closed), you may get an error. If you need to kill a background process after you have started it, you can use the UNIX kill command. Use the UNIX ps command to find the process id of your server. Make sure you perform error handling and close every socket that you use in your program. On Linux systems, you can run the command "netstat" to see which port numbers are currently assigned. Networks - Project 1
8
Building a Multi-Threaded Web Server
In this Project you will develop a Web Server and a Browser (client). Your multi-threaded Web server will be capable of processing multiple simultaneous service requests in parallel. Your browser will be able to print out the text of the data sent to it. You will demonstrate that your Web server is capable of delivering various page types to a Web browser. You are going to implement version 1.0 of HTTP, as defined in RFC 1945, where separate HTTP requests are sent for each component of the Web page. 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. Networks - Project 1
9
Overview Of Operation Web Browser Your Web Server Connect Send Receive
Accept( new_socket ) Create_thread New Thread Receive Send Networks - Project 1
10
Building a Multi-Threaded Web Server
This could be a daunting project. To simplify this programming task, you will develop the code in multiple 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 your code, you can test your server with a Commercial 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 xx.clarku.edu, and if your server is listening to port 6789, and if you want to retrieve the file index.html, then you would specify the following URL within the browser: xx.clarku.edu :6789/index.html If you omit ":6789", the browser will assume port 80 which will either not have a server listening on it or will have apache listening – if apache is running on the target machine. 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. Networks - Project 1
11
Web Server Development
In the following steps, we will go through the code for the first implementation of our Web Server. Our first implementation of the Web server will be multi-threaded, where the processing of each incoming request will take place inside a separate thread of execution. This allows the server to service multiple clients in parallel, or to perform multiple file transfers to a single client in parallel. When we create a new thread of execution, we need to pass to the Thread, information about the socket that’s just been accepted from the remote browser. See the diagram on the next page to better understand this. Networks - Project 1
12
Web Server Skeleton Networks - Project 1
For your amusement and edification, the code for a working multithreaded program is given here. You would be wise to run that program just to see how it works. And you would also be wise to use this code as the starting point for your web server. You can then insert network-specific code into this multithreaded example. The skeleton of your Web server is contained in that multithreaded example from the file. #include <stdio.h> #include <pthread.h> #include <sys/resource.h> #include <asm/errno.h> int WorkThread( void * ); // Prototype unsigned int CreateAThread( void *, int *); // Prototype // /////////////////////////////////////////////////////////////////////// // This is the main() code - it is the original thread main() { unsigned int CurrentPriority, MyPid; unsigned int NewThreadID; // Create a new thread that will start executing at location WorkThread NewThreadID = CreateAThread( (void *)(*WorkThread), &data); // Other main code } // End of main // This is the new thread that's created int WorkThread( void *data ) } // End of WorkThread Networks - Project 1
13
Web Server Skeleton Next, we open a socket and wait for a TCP connection request. Because we will be servicing request messages indefinitely, we place the accept operation inside of an infinite loop. This means we will have to terminate the Web server by pressing ^C on the keyboard. // Get the port number via the command line // Establish the socket that will be used for listening fd = socket( ????? ) // Do a bind of that socket bind( fd, ???? ) // Set up to listen listen( fd, ??? ) fdListen = fd; while( TRUE ) { // Do the accept fdconn = accept( fdListen, ???? ) } Networks - Project 1
14
Web Server Skeleton Networks - Project 1
When a connection request is received, we create a thread, passing to that thread a reference to the new Socket, fdconn, that represents our established connection with the client. The thread creation routine wants data of type (void *) so we cast fdconn going into the routine, and then cast it back when we get into WorkThread. while(TRUE) { // Do the accept fdconn = accept( fdListen, ???? ) out = CreateAThread( (void *)(*WorkThread), &fdConn); } // End of while } // End of main // This is the new thread that's created int WorkThread( void *data ) int fdConn; fdConn = (int)data; } // End of WorkThread After the new thread has been created, execution in the main thread returns to the top of the while loop. The main thread will then block on accept, waiting for another TCP connection request, while the new thread continues running. When another browser request is received, the main thread goes through the same thread creation regardless of whether the previous thread has finished execution or is still running. Networks - Project 1
15
Web Server Skeleton Now, let's develop the code within WorkThread. Here are the steps we need to follow: We alloc an input buffer to hold the data we’re getting from the client – zero it out. Then we get the client’s request message which we do with a recv(). The amount of data taken in will be the lesser of the amount of data in the system ready to be received, OR the size of our buffer. Recv returns the number of bytes it’s received so we keep reading until nothing more is taken in. We could take in 1 byte or we could take in up to BUFFERSIZE bytes with each recv – we don’t know so we just keep reading until we don’t get anything more. Networks - Project 1
16
Web Client / Browser Continuing with the outline shown at the beginning, your next task is to build your client. Begin by getting the starter code. One of your tasks requires that your command line accepts a URL OR an IP address – here’s some sample code for your amusement. You might as well also get the file Gettysburg.html that you will use to demonstrate that your system is successful. You will be using your own .jpg file – something of interest to you, but you may want to also download the file I used. The starter code gives you a great outline of what you need to do. Find resources that will guide you to fill in the missing pieces. From your previous work, you know what a browser request looks like – your server has been printing that out for you. Design this client so it produces a request formatted the way a server wants it. When you use your client to look at a website you can see the format that a server uses to send information back to the client. Networks - Project 1
17
Web Server – Part 2 Instead of simply terminating the thread after displaying the browser's HTTP request message (as done previously), you will analyze the request and send an appropriate response. Here are the steps you might follow: Check that the client really gave us a “GET” request. Extract and massage the filename the client wants to see. Check out the C library routines Strncmp, Strcpy, Strncat, and Strchr. You may find them useful. After you’ve extracted the filename from the browser string, print it out as you have been doing. (Make sure that you precede the filename with a “./” so that the file is required to exist in your current directory – otherwise the browser could be asking for any file in your entire directory structure.) If the file does not exist, pass back a 404 error message to the client. If the file, Gettysburg.html, does exist, prepare a header and then pass back the file to the client. (You will be reading from a file and writing to the network. The client should be prepared to print out the contents of the file that it receives from the webserver. Networks - Project 1
18
Web Server – Part 2 When the file exists, we need to determine the file's MIME type and send the appropriate MIME-type specifier back to the client.. We make this determination by examining the file’s extension. which returns a string that we can include in the content type line that we are constructing. A file with extension .html has MIME of "text/html“ A file with extension .ram has MIME of "audio/x-pn-realaudio“ A file with extension .jpg has MIME of “image/jpg“ You will have figured out how to use this information by examining the headers being sent back and forth between client and server. This completes the code for the second phase of development of your Web server. Try running the server from the directory where your Gettysburg.html and your .jpg are located, and try viewing the .html file with your browser. Networks - Project 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.