Transcript ppt

Programming Project #3
Web Server
CS-3103 & CS-502
Operating Systems
CS-3103 & CS-502,
Summer 2006
Programming Project #3
1
Programming Assignment
1. Build a simple but real web server using
sockets
2. Build a simple web client for testing your
server
3. Make your server fork a process or
spawn a thread for each new request
CS-3103 & CS-502,
Summer 2006
Programming Project #3
2
Purpose
• To gain some insight into sockets and
communication over networks.
CS-3103 & CS-502,
Summer 2006
Programming Project #3
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 protocol
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.
CS-3103 & CS-502,
Summer 2006
Programming Project #3
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 ask the listener of port #4242 to return the file at
/www/docs/admissions.html
CS-3103 & CS-502,
Summer 2006
Programming Project #3
5
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.
• Use it to gain insight into details of HTTP
protocol
CS-3103 & CS-502,
Summer 2006
Programming Project #3
6
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
CS-3103 & CS-502,
Summer 2006
Programming Project #3
7
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!
CS-3103 & CS-502,
Summer 2006
Programming Project #3
8
Sample code
• See
http://www.cs.wpi.edu/~cs502/s06/CodeFragments/
• Includes
• sockserver.c
• sockclient.c
• sockreadline.c
CS-3103 & CS-502,
Summer 2006
Programming Project #3
9
Part 2 – Multi-process or multithreaded web server
• Modify your web server to fork() a process or
spawn a thread 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.
CS-3103 & CS-502,
Summer 2006
Programming Project #3
10
Project Submission
• Project due at start of class on June 29
• (i.e., last class of the term for CS-3013 students)
• Submit via turnin
• command = ‘/cs/bin/turnin’ on CCC machines
• classname = ‘cs502’
• assignment = ‘project3’
• Include
• Code, makefiles, test files or input, test output
CS-3103 & CS-502,
Summer 2006
Programming Project #3
11
Format of submission
•
•
•
•
•
Write-up
Makefile to make all, clean, each part
Source code files (.c, .cpp, .h, etc)
Any test cases
Any output logs
• All at top level:
• No subfolders
• No zip files
CS-3103 & CS-502,
Summer 2006
Programming Project #3
12