Lab 5 - Handling Multiple Clients

Due: Monday, May 11, 2009 at 1:00pm

For this lab, we will be looking at daemon.c from the code tarball. Compile the program with the command:

make daemon

The daemon program is a very simple TCP/IP server that will host up to MAX_CHILDREN concurrent clients. It will take whatever is typed in the client application and either print it to screen or optionally print it to file if the -log option is specified. The program name comes from a Unix naming convention which calls servers "daemons" (hence many server programs having the letter "d" in their name, such as sshd, named and so on). This is a very simple, unauthenticated, plain-text Internet daemon.

To start daemon in the default mode, which prints client data to the screen, use the command:

./daemon
To start the code in logging mode, which will write the client data out to a log file, use the command:
./daemon -log
Try the following first in default mode, then close daemon and repeat the following in logging mode.

The program will display the port number to which daemon is bound. Open another terminal and try to telnet to the daemon. For example, if it says it is on port number 19234, the command would be:

telnet localhost 19234
To end the connection, within the telnet application give the keyboard sequence CRTL-]. This causes telnet to suspend the connection and give you a telnet shell which looks like telnet>. From here, you can give the command "close" to terminate the current connection, the command "open <hostname> <portnumber>" to open a new connection (after closing the current one) and the command "quit" to exit.

To kill the daemon process, return to the terminal you were running it in and give the CTRL-C sequence. daemon.c has a signal handler for CTRL-C which will call parent_terminate(). This function cleans up after the process before exiting.

Try running daemon with just one telnet connection at first. Then run daemon with multiple telnet connections at the same time (you will need one terminal for each connection). Pay attention to what is printed to the daemon screen when children connect and disconnect. If running in log mode, you will not see what is typed in the telnet sessions, but you can view the logs once daemon has completed.

Lab Writeup

Answer the following questions as your lab writeup. Email the writeup to me.
  1. Unlike vcrec.c, which could only accept a single connection at a time, daemon.c can accept multiple connections at once. How does it accomplish this?
  2. Which signals will cause daemon.c to exit gracefully via parent_terminate()?
  3. Why does the ctrl-c signal not work to kill the telnet application?
  4. In dialog_with_telnet(), the actual work of sending and receiving data from the children processes is done. How does this code tell when you have terminated the connection?
  5. What is done to clean up a child process/thread upon termination?