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:
./daemonTo start the code in logging mode, which will write the client data out to a log file, use the command:
./daemon -logTry 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 19234To 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.