Homework 2 - A Home-Brew Multi-Threaded Web Server

In this homework, we'll do the server end of http. First export the template in svn://cs450.cs.uic.edu/pub/homeworks/hw2 and add it to your own repository. This time around, the template only contains a Makefile.

Based on examples/sockets/server.c, create a web server application that takes a port and a directory name as arguments, and serves the contents of this directory over http. For example, after building hw2, you should be able to run

./hw2 8080 WWW

and then visit http://localhost:8080/index.html, to see the simple test page in the WWW folder of the hw2 template. Clicking on the link should take you to a second page, than contains an image. You may change the files in the WWW directory, as long as there is at least two pages connected by a link, and at least one image embedded in a page.

You need to handle the following cases:

  • non-existing file: return error 404 with a readable error message, just like a webserver would
  • html, text, jpeg, gif and png files should all display properly. return the proper HTTP Content-type header, based on the file ending. Also, pdf files should be handled correctly.
  • if the requested path is a directory, you should handle request as if it was for the file index.html inside that directory. hint: use stat() to determine if a path is a directory or a file. the st_mode field in the stat struct has what you need.
  • some clients may be slow to complete a connection. Your server should be able to serve multiple clients concurrently, not back-to-back. For this homework, use multithreading with pthreads. We'll try event-based concurrency in hw4.

Note that if you use port 80, there is no need to indicate the port in the URL, and you should also be able to fetch the pages with hw0. However, port 80 requires administrative privileges on your machine, which you may not have, depending on what machine you are doing the homework on.

HINT: useful libc functions to use are stat(), strstr()/strcasestr(), sprintf(), sscanf(). The code in examples/sockets/server.c should be very helpful. My entire solution, starting from the 68-line server.c, with lots of comments and nice formatting, is 124 lines. Think before you code and try to keep your code manageable.


Commit your finished homework at:


where, obviously, yournamehere should be replaced by your UIC netid. To ensure that your submission is complete, follow the procedure that we will use when grading your homework.

First, in a brand new, empty directory, do the following:

svn export svn://cs450.cs.uic.edu/s13/yournetid/hw2
cd hw2
./hw2 8080 WWW
and now visit http://localhost:8080/ and click the link, using firefox and perhaps one other browser.

Once everything looks right to you, run the automatic grading script in pub/grading-scripts/hw2.


Grading will be done automatically using a script. You can check your grade before final submission using the script. You can get the grading script using:

svn co svn://cs450.cs.uic.edu/pub/

or just svn up in your /pub if you already have one checked out. The grading script is in /pub/grading-scripts/hw2/. To run the grading script:

./hw2_score username port score_file

This script will create a directory with your username, download hw2 from svn and create output_file and score_file inside hw2. Check the score_file to see how your solution is doing.

This topic: CS450 > WebHome > Homework2
Topic revision: r5 - 2013-01-24 - 23:30:27 - Main.ckanich
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
Helping Women Faculty Advance
Funded by NSF