spring 2003
homework #3
due monday march 31, by 6am (electronically)

The goal of this assignment is to learn how to write two programs in C that communicate with each other via stream sockets using TCP.

This assignment is worth 10 points. Distribution of points is indicated below.

We are working toward building a game that will consist of two parts: a server that will run locally on a CS machine and a client that will run on the machine belonging to the person(s) playing your game. In this assignment, you will take the C program you wrote for hw#2 and divide it into two programs -- one which will be the server and the other which will act like the client.
(Note: your client will be replaced with a Java application in hw#4 and a Java applet in hw#5).

You need to decide which components of hw#2 belong in the server and which belong in the client:

You will need to define a little language for your client and server to communicate with each other. You need to think about what kinds of information will be sent back and forth. For this assignment, you only need to communicate login information. In later assignments, the client and server will need to communicate about elements of the game. Here are five commands to start your language:


FIRST: print out the timesheet and keep track of how much time you spend doing the tasks outlined below.

1. design.(2 points)
As usual, you need to begin with a design document.
For this assignment, you design document must describe the flow of data between the data files, the user, the client and the server.
A picture is worth a thousand words (and 1 point).
You need to include in this description not only information on the data flow for the login procedure, detailed above, but also the information on the data flow for the game (which you will implement in a later assignment). So your description here should include the other commands you anticipate needing for your client-server language (1 point).
If you make any changes to the game as described in your first homework, you must document these changes here.
This documentation should be 1-2 pages, 10-point font. It must be complete -- so you need to be CLEAR and CONCISE. You MUST print out the documentation and bring it to class on the due date.

2. build the server. (3.5 points)
Start with my server.c sample program and your hw#2. Extract the functions from your hw#2 that interact with the data files. You should make use of the header file you created for hw#2.
The server should do the following:

  1. As in the sample server: (0.5 points)
    1. Create a socket and bind it to a port (assigned by the system).
    2. Listen on the socket for connections.
    3. Get the port number assigned by the system and output that to the screen (so you'll know the number for when you start up the client).
    4. Accept a connection from the client.
  2. Wait for input from the client, and when a message is received, parse it and process it. (0.5 points)
  3. When a LOGIN command is received, the server should check if the username and password match what is in the "users.dat" file. If it matches, then the server should send an OKAY command to the client. If it doesn't match, then the server should send a BAD command to the client. (1 point)
  4. When a NEW command is received, the server should act like in hw#2 where you create an entry in "users.dat" and a data file for the new user. Then the server should send an OKAY command to the client. (1 point)
  5. Then the server should wait for the client to finish executing. The client will send a BYE command, so the server should wait until it receives one. Then the server can exit. (0.5 points)
Name your server: hw3server<your-cs-username>.c, so for example, mine is called hw3serversklar.c.

3. build the client. (3.5 points)
Start with my client.c sample program and your hw#2. Extract the functions from your hw#2 that interact with the user.
The client should do the following:

  1. As in the sample client: (0.5 points)
    1. Get the port number from the command line.
    2. Create a socket and connect to the server using the port number specified on the command line.
  2. Call a login function that prompts the user for her username and password and reads her username and password. (0.5 points)
  3. Send the username and password from the client to the server, for validation, using either the LOGIN or the NEW command. (0.5 points)
  4. Wait until the server responds, with either an OKAY or a BAD command. (0.5 points)
  5. If the server sends an OKAY command, then print a "yippee" message to the user, send a BYE command to the server and exit. (0.5 points)
  6. If the server sends a BAD command, then print an error message and loop back to step 2, or create a new user (by sending a NEW command). When creating a new user, the server will respond with an OKAY command. (0.5 points)
  7. Once the server responds with an OKAY command, the client should send a BYE command to the server and exit. (0.5 points)
Name your client: hw3client<your-cs-username>.c, so for example, mine is called hw3clientsklar.c.

4. submit. (1 point)
You need to submit your program in two ways: electronically and hard copy.

  1. electronically
    Follow the submission instructions.
  2. hardcopy.
    (1) Print out the homework template cover page and fill it out.
    (2) Print out the timesheet and fill it out.
    (3) STAPLE the cover page, timesheet and hardcopy of your documentation together.
    (4) Make sure your name and CS username are on all the papers you hand in!
    (5) Bring all this to class on the day the assignment is due and deposit it in the homework box at the front of the classroom within the first 5 minutes of the class (by 1.15pm).