CIS 432/532 Introduction to Computer Networks
Fall 2002

Program #1: Basic TCP Socket Programming in C/C++
Due October 15th, 2:00 PM, online and in class

Overview

You will write a basic TCP client/server program that allows a client to contact the server and ask for the current time. You will write the server so that it can handle more than one client at a time. You will write the client so that it keeps a connection open to the server until the user is done. Your program must be written in C or C++.

Client User Interface

When the client is started, displays a prompt "%" and the user can type several commands. Each line the user types ends when a carriage return is entered; the client then sends this command to the server and waits for a response.

The commands a user may type are:

  Command Response
  time Prints the current time in a readable text format.
  diff Determines how much time has elapsed since the last time the client asked for the time. Prints the difference in a readable text format (H hours, M minutes, and S seconds).
  quit Exits the program

If the user types anything else, print "Huh?" and then another prompt.

Here is an example of what the client should be able to do:

> timeclient -s ix -p 5232
%time
Wed Oct 2 16:03:41 PDT 2002
%diff
10 seconds
%diff
1 minute and 42 seconds
%times
Huh?
%time
Wed Oct 2 16:08:21 PDT 2002
%quit
>

Time Protocol

As with most programs, the user interface is specified separately from the client-server time protocol. However, to make your job easier, the messages are identical. This means you should be able to take a message typed by the user, check to be sure it equals "time" or "diff" and then send it to the server. Likewise, you should be able to take any response from the server and print it for the user.

Our main goal is to standardized the time protocol so that different client and server implementations can talk to each other.

Each message in the time protocol contains a sequence of characters that is terminated by a newline character ('\n'). The client messages (and the server responses) are:

  1. time

    Get the current time. Server responds with the current time in readable text format as defined by the Unix call to ctime().
  2. diff

    Get the time elapsed since the client last asked for the time. Server responds with the difference in hours, minutes and seconds. If the number of hours is zero, then it is skipped. Likewise, if the number of minutes is zero, then it is skipped. You should also change "hours", "minutes", and "seconds" to the singular form when necessary.

If the server gets a message it does not recognize, it ignores it. If you implement your client properly (checking messages before they are sent), this should never happen.

Client and Server Implementation

The client uses only one socket for its entire lifetime.

The server must do a number of tasks:

You should not worry about making the server multi-threaded. A single thread of control is fine. However, you will need to do some relatively complex socket programming to handle input and output with all of the sockets from the clients. In particular, you will need to allow simultaneous connections from any number of clients. Be sure to read about the select() system call in the socket programming book and in the online manual.

Command Line Arguments

Your C executables must be called timeserver and timeclient.

The server should take the following arguments:

  Argument Definition
  -p [port] Port number of the server.
  -d Print debugging information.

The client should take the following arguments:

  Argument Definition
  -s [server] Machine name of the server (i.e. ix.cs.uoregon.edu).
  -p [port] Port number of the server.
  -d Print debugging information.

By passing these arguments on the command line, you can try running your server on different machines and ports. You can use the debugging flag to print out any helpful debugging information you want, but you should never print debugging lines if the flag is not specified on the command line.

Error Checking and Debugging

You should check for errors resulting from system calls. If a system error occurs, use the perror() interface to print out its corresponding code and then immediately exit the program. You must use the debugging interface defined in debug.h to print out any debugging comments. This way you can turn off the extra comments via the command line.

Compiling and Running

You must supply a Makefile that will compile the C/C++ client and server when the user types make. The executables must have the names listed above.

Suggestions and Sample Code

You should write the server to first handle a single client correctly. Then implement select() to allow it to talk to multiple clients at the same time.

You can find sample C code in the class directory at /cs/classes/cis432/program1.

Grading

Your programs will be tested in two ways. First, your client and server will be tested with each other. They should operate as described in this assignment. Second, your client and server will be tested with a solution client and server. Your client should be able to talk to the solution server, and the solution client should be able to talk with your server.

If you wish to receive partial credit for a program that does not operate correctly, you must print out clear and concise debugging information when the debugging flag is given on the command line. I will use this information to determine how much of your program works and therefore how much partial credit you will receive. The more helpful and clear your debugging information is, the better your chances. You should apply this same standard to how you document your code.

Any program that does not compile or that crashes while running will receive a 0.

Turning in the Assignment

You must submit all your source code online. Do not submit any object code. Use the submission instructions for this program, which are available in the Schedule section of the class web page.

You must also print out and fill in the survey for this program. The survey is also available in the Schedule section of the class web page. Turn this in to my office on the due date (or put it under my door).