Lab 6: Network Servers

This lab focuses primarily on the functionality for setting up and operating a server. Section 4.8 of the book illustrates much of this functionality. In addition to general I/O functions (such as close(), read(), and write()), you should consult the C documentation as needed for the following functions:

  • accept(), bind(), inet_ntoa(), listen(), setsockopt()

Preliminaries

Set up a bare repository on stu.cs.jmu.edu based on the instructions in the CS 361 Submission Procedures, using the name lab6-www.git.


Implementation requirements: Set up a minimal HTTP server

Most of the work will involve modifying utils.c to implement a number of helper functions:

  1. Complete the code in build_address() to set up an IPv4 address within a struct sockaddr_in.
  2. Using the textbook examples as a reference, set up a server socket in setup_server(). This function should go from making the call to socket() until the call to listen(). Return the socket file descriptor (or -1 if an error occurs).
  3. Complete get_connection() so that it accepts incoming requests and prints the IP address of a connection. Use "Received incoming request from %s\n" for the format string for printing the address. Return the file descriptor for the connection.
  4. Complete build_response() so that it opens the file, uses buile_response_header() to build the header, then concatenates the contents. If the file doesn't exist, return NULL.

Next, complete the implementation of serve_web() in server.c. The provided code uses the functions in utils.c to set up the server socket and get the connection. You will need to complete the code to read the request, call build_response() to get the HTTP response, then write the response back to the client. If build_response() returns NULL, then write the 404 message indicated. Finally, close both the server and connection sockets and shut them down.



James Madison University logo


© 2011-2024 Michael S. Kirkpatrick.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.