Multi-Threaded Chat Server

You may work alone or with one other classmate on this assignment, and each person is expected to contribute equally to the effort.

Introduction

In this project, you will build a simplified chat server that can support multiple clients over the Internet. There are a gazillion chat programs out there using various protocols, with IRC (Internet Relay Chat) being one of the earliest and most popular chat protocols. We will implement a very simplified version of IRC.

You may use the echo client and server as starting point for this assignment, as described below.

Starting Point: The Echo Client/Server Application

  1. Log into tanner. If you have already copied the echo client/server source code in your systems/sockets Unix directory, skip this step. Otherwise, in the terminal window, type in
         /mnt/a/mdamian/bin/install_echo.sh
    
    and then hit Enter. This script creates a directory called sockets in your systems directory and copies the following files in your ~/systems/sockets directory:
        echoserver.c  C code for a sequential echo server
        echoclient.cC code for an echo client
        nethelp.cFile containing helper functions
        nethelp.h Header file for functions defined in nethelp.c
        MakefileCompiles and links together nethelp.c and a source file

    Make sure the systems directory (created at the beginning of this semester) exists before running this script.

  2. Build the xechoserver and xechoclient executables by typing in make at the shell prompt.

  3. To avoid clashing with one another, each of your servers must listen to a different port number. Check this list to find out the port number assigned to your server.

    Invoke the server with your designated port number N as an argument:

         ./xechoserver N
    Open a separate terminal window to test the server with telnet first. At the shell prompt, type in
         telnet tanner N 
    Now anything you type into the telnet window will be sent over the connection and echoed back to you by the server.

  4. Next test both the echo client and echo server and by having them communicate with each other. Restart your server, then in a different terminal window type in
         ./xechoclient tanner N
    The behavior is the same: anything you type into the telnet window will be sent over the connection and echoed back to you by the server.

  5. Try to understand what each step of the client and server code does.

  6. Modify echoclient.c so that the client quits when the user types in exit.

  7. Modify echoserver.c to have the server create a new thread each time it receives and accepts a connection request from a client (one thread per client). The new thread will provide service to the client, while the server gets back immediately to listening for new incoming requests from other clients.

    Note that the server will have to pass to the thread the file descriptor for the connection returned by the accept system call, so that the thread can use it in its communication with the client.

    To test your multithreaded server, open two client windows and have the clients talk simultaneously to the server.

Next Step: The Chat Client/Server Application

The next step is to extend the code for the echo client and server to implement a chat application. Users should be able to join the chat server after entering their names, broadcast messages to all other users, and leave the chat room anytime.

Work incrementally, one step at a time, one command at a time. Make sure to thoroughly test and debug one command before moving on to the next.

  1. Copy echoserver.c to chatserver.c and echoclient.c to chatclient.c. Modify Makefile to work with the new source files (chat* instead of echo*).

  2. Modify chatclient.c and chatserver.c to implement the following communication protocol (the client reads in commands from the user and forwards them to the server):

  3. The server should be multi-threaded: for each client connection, the server should spawn a new thread to interact with the client.

  4. Make sure you protect critical sections with semaphores.
If your client and server meet the above specifications, they should be able to connect to the other clients and servers in the class.

Submission Instructions

In your sockets directory create a text file named readme (not readme.txt, or README, or Readme, etc.) that contains:
  1. Your name(s) and assignment number.
  2. The machine you used to complete the assignment (i.e, tanner or your own machine). If you used your own machine, please be ready to demo your code to your TA or your instructor at any time upon request.
  3. A description of whatever help (if any) you received from others outside of class.
  4. An indication of how much time you spent doing the assignment outside of class.
  5. Your assessment of the assignment: Did it help you to learn? What did it help you to learn? Do you have any suggestions for improvement? Etc.
  6. Any information that will help us to grade your work in the most favorable light. In particular you should describe all known bugs.
Descriptions of your code should not be in the readme file. Instead they should be integrated into your code as comments. Your readme file should be a plain text file. Do not create your readme file using Microsoft Word or any other word processor.

Turn in a printout copy of the readme, chatclient.c and chatserver.c files, and a sample output (one printout per team). If you work on the department Unix system, you will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.

Leave the source code for all exercises in your directory systems/sockets. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory sockets using the following Unix command:

    cp -r ~/systems/sockets ~/systems/sockets-copy
A new directory called sockets-copy will be created in your systems directory. You may now make any changes you want to the files in your sockets-copy directory, at any time.

Grading

No credit will be given for code that does not compile.
100     Total points possible
 
 20 Program compiles and runs without crashing
 35 Server functions properly, handles multiple clients
 35 Client functions properly, handles all communication with the server correctly
   5 Submission includes readme file
   5 Submission includes sample output
+20 Implement the multi-threaded chat application in Java as well

Have fun!