Simple Chat Server


The goal of this assignment is for your group to gain some experience in client/server protocol design and programming using the BSD socket API.

Assignment

You are to create a server and two client programs to support a simple chat-room style network application. The resulting programs will allow any number of people throughout the world to participate in an interactive on-line discussion. The final application consists of a chatserver called schats(simple chat server) and two clients: listen and post. You may use any language you wish to create these three programs, but they must run on the machines in the LCL. Of course, class examples and my hints will assume that you are using Python.

When a chat server is started, it is given a port number on which it listens for client connections. Thus, to start a server running on port 2001, we would use the command: schats 2001 &. The ampersand (&) causes this command to run in the "background", thus not tying up the console in which it was started.

The listen command is used to start a client that listens in on converstations that are going through the chat server. To start listening, we have to provide the hostname and port of the chat server. We might invoke it this way: listen clown.wartburg.edu 2001. Now any comments that are sent to the chat server that is running on port 2001 of clown.wartburg.edu will be printed in the console where the listen command was started.

To actually contribute to a conversation, you would use the post client. When starting post, you must supply the location of the server, just as for listen. In addition, post requires a "handle," a short name that identifies your contributions to the conversation. Thus, we might start the program like this: post clown.wartburg.edu 2001 pyfan. This program then enters a loop of getting a line of user input and sending it to the chat server on clown. The listening clients will see the each line prefixed with the name "pyfan"

Hints/Resources

Creating Python Programs in the LCL

You can use either Emacs or Idle to develop your programs. Python programs normally have the extension ".py", and both of these editors will work better if you use this extension on your filenames during development. Once you have completed the programs, you can rename them to get rid of the ".py". Eclipse also has a Beta Python mode that seems useable.

Running Python Programs in the LCL

You can run a Python program on Linux by invoking the Python interpreter on the command line, e.g. :  $python schats 2001 &. You can make the program directly invokeable using the Unix "pound-bang" trick. At the top of the program, put a command that describes where the Python interpreter can be found: #!/usr/bin/env python.Then make sure that the file is executeable. You can do this either by right-clicking the icon and setting the permisions in the pop-up window, or by using a command like: $chmod +x schats.

Handling Command-Line Arguments

All three component programs require you to process command-line arguments. These are stored in an array of strings called argv that lives in the sys module. The 0th argument is always the name of the command itself that was given on the command line. Here is a program that "echoes" the strings typed on the command line:

#!/usr/bin/python
#echoargs.py

import sys

def main():
    for arg in sys.argv:
        print arg

main()
Note that all the components of argv are strings. If you want to treat a command-line argument as a number (e.g. port), you will need to convert it to numeric form (using int).

Handling Exceptions

Errors in programs may raise exceptions. For example, if you try to write information to a socket that has been closed, the socket will raise an exception. Handling these gracefully requires using the Python exception mechanism try..except. You can get the skinny on this construct from the relevant section of the Python turorial here.