Transcript ppt

Programming Project #4
Web Server
CS-502 Operating Systems
Spring 2006
CS502 Spring 2006
Programming project #4
1
Programming Assignment
1. Build a simple but real web server using
sockets; also build a simple web client for
testing your server
2. Make your server fork a process for each
new request
3. Make your server spawn a new thread for
each request
CS502 Spring 2006
Programming project #4
2
Purpose
• To gain some insight into sockets and
communication over networks.
CS502 Spring 2006
Programming project #4
3
Part 1 – Web Server
• Create a socket() and bind() it to a port.
• listen() on that port for requests
• Wait for requests; upon receipt of a request
–
–
–
–
–
accept() the request
Interpret the HTTP request
Find and deliver the page; or return an error
Close the accepted request
Loop back and wait for another request
• Figure out a way of exiting from the server
cleanly.
CS502 Spring 2006
Programming project #4
4
Part 1 – Web Server (continued)
• Command line:% server <directory> [optional port #]
• Directory is prefixed to every request.
– WPI directory for web pages is /www/docs
• Port is port number for your server
– You may specify a default port in your code, but don’t choose one
to conflict with another student!
– Warning: Unix does not let you reuse ports until after a reasonable
waiting period (30 seconds?)
• A request to your server of
http://ccc4.wpi.edu:4242/admissions.html
means return the file at
/www/docs/admissions.html
on port #4242
CS502 Spring 2006
Programming project #4
5
Class discussion next week
• Review experience with using sockets to
actually do something.
• Discuss how to exit server program cleanly.
CS502 Spring 2006
Programming project #4
6
Part 1 – Web Client
• Simple test program that requests web
pages and prints out their contents.
• Use to see what your server is doing.
• Use to see what another server is actually
serving when you request the same pages.
CS502 Spring 2006
Programming project #4
7
HTTP commands
• Your server must interpret HTTP GET
command
• See written project description for examples
• All command lines must end with CR/LF
– i.e., in Unix \r\n
• Series of HTTP commands ends with blank
line
– i.e., a stand alone \r\n
CS502 Spring 2006
Programming project #4
8
HTTP Response
• Once you accept a command and have found the
page, respond with HTTP success response 200
– See written project description
– Be sure to follow with a blank line
• If you cannot find or read the page, respond with
HTTP error response 404
• You only need to serve real file pages.
– Ignore scripts, etc.
• WPI’s home page is a script.
– Remember: some pages are not all text!
CS502 Spring 2006
Programming project #4
9
Sample code
• See
http://www.cs.wpi.edu/~cs502/s06/CodeFragments/
• Includes
• sockserver.c
• sockclient.c
• sockreadline.c
CS502 Spring 2006
Programming project #4
10
Part 2 – Multi-process Web Server
• Modify your web server to fork() a process each
time it receives a request.
• This allows it to serve multiple requests in
parallel.
• Test with multiple web clients or browsers in
parallel.
• Be sure to clean up “zombie” processes on the fly
– Use wait3()
• Be sure that your server leaves time for each
process to finish before exiting cleanly.
CS502 Spring 2006
Programming project #4
11
Part 3 – Threaded Web Server
• Modify your web server to spawn a thread each
time it receives a request.
• This also allows it to serve multiple requests in
parallel.
• Test with multiple web clients or browsers in
parallel.
• Be sure to threads exit cleanly after finishing with
each connection.
• Be sure that your server leaves time for each
thread to finish before exiting cleanly.
CS502 Spring 2006
Programming project #4
12
Project Submission
• Project due at start of class on April 17
• (i.e., one week before the last class of the term)
• Submit via turnin
• command = ‘/cs/bin/turnin’ on CCC
machines
• classname = ‘cs502’
• assignment = ‘project4’
• Include
• Code, makefiles, test files or input, test output
CS502 Spring 2006
Programming project #4
13