Department of Computer Science

COSC 4377 - Introduction to Computer Networks
Section 07925
Fall 2000

Assignment 2 - Socket Programming

Due on Monday, October 16, 2000

This programming assignment is designed to let you familiar with BSD sockets , client-server application, and persistent connection. This assignment weights 200 points.

The usual note:

Latest Hint:

Please see FAQ.


You will write two C/C++ programs using BSD sockets (using AF_INET and SOCK_STREAM) to implement a client and a server which follow a protocol (called Oracle) to exchange message and perform some functions on server. There will be only one client and one server for this assignment.

  1. After connection between client and server is established, client sends a "Hello" to server.
  2. Server responses with an "OK" and a random session id (client will use this session id to communicate with server).
  3. Then client sends the user name, and server responses with "HI username".
  4. Client sends a question to server and server responses with an answer. The sample questions are "server name", "date/time", "user name", "add 1 2", "subtract 4 2", etc.
  5. At the end of message exchange, client sends "Bye" to server and server responses with "Bye username". And then connection is closed.
  6. If client sends another "Hello", server issues another random session ID. And continue as (3).
  7. Server keeps track of client's hostname, current user's name and session ID. If client's session ID doesn't match current session ID at server, server sends an error message and terminates the connection.
  8. Each message includes a unique ID (4 bytes, first 2 letters of your last name + first letter of your first name + last digit of your SSN). Client and server has a copy of this unique ID. If server receives a message with different unique ID, server responses a error message and closes the connection.
  9. If any command received by server is out-of-order (for example, server receives "ASK" command as the first massage from client), server responses an error message and terminate the connection.

Oracle Protocol

Message format

Each message has 42 bytes which are consisted of 4 parts.

Messages exchanged between client and server

The messages exchanged between client and server are listed (but not limited to) in the following table. You can add extra error messages to cover out-of-order commands.

Sent from
HELO from clienthost
send client's hostname (clienthost) to server
USER username
send client's current user name (username)
request server's hostname
request current session ID
request client's hostname
ASK USER request current user's name
request date and time on the server
ASK ADD numbr1 numbr2
request the result of (numbr1 + numbr2)
ASK SUB numbr1 numbr2
request the result of (numbr1 - numbr2)
notify the end of connection
reply with session ID (SNID)
HI username
acknowledge current user (username)
ANSW result
reply with the result requested by ASK command
BYE usrname from clnthost
acknowledge the termination to current user (usrname) at client (clnthost)
ERR unknown command
server receives an unknown command
ERR incorrect UNIQ ID
server receives mismatched unique ID
ERR incorrect SNID
server receives mismatched session ID
...... ......

The state diagram of messages exchanged between client and server is described in the following feature.

There were some confusions regarding the state diagram above. So I drew another state diagram to make the transition between different states easier to understand. Start, C2, C3, and End are on the client; S1, S2, S3, and S4 are on the server. "Error" state indicates server receives a message containing error(s) and labels on the edges reaching "Error" state indicate the messages sent to client.


Discussion Questions:

  1. When client receives message from server, client finds the unique ID from server is different from client's (somehow another server hijacks the connection and uses different unique ID.) Should client close the connection right away, or sent a message to server before close the connection? Which way is better? Why?
  2. Why does this protocol provide persistent connections?
  3. When server receives an out-of-order command, it would be better to tolerate the error and continue the execution (instead of terminate the connection). Give an example (or sequence) that server receives an out-of-order command and continues execution and still preserve the authentication sequence ("HELO" and then "USER"). You may add extra message to achieve fault-tolerance.

Submit your assignment: (updated)


Last modified: October 12, 2000.
tihuang at cs . uh . edu