Stanford CS240h Lab 2

You will write a basic chat server. It will support any number of clients and allow them to join and leave at any time. Please implement it as follows:

We are providing a skeleton Cabal project to help get started, download it from here.

Example Transcript

1 has joined
2 has joined
1: hello!
2: hi
3 has joined
3: hi
1 has left
2: hi 3


As a bonus, you can implement rooms and private messaging. All clients should connect initially to a default room so that testing of the standard assingment still works.

The syntax for changing to a new room is:

/join <room name>

i.e., /join haskell will cause the client to change to the 'haskell' chat room.

Please ensure the command is valid (i.e., only contains 1 argument), if it isn't please respond to the client with:

server: invalid /join command

Rooms should function as if they were independent servers. So when a user U leaves rooms R1 and joins room R2, two things should happen:

The syntax for messaging is:

/msg <username> <message>

i.e., /msg 12 hello world will send a private message to client 12.

Please ensure the command is valid, if it isn't then respond to the client with:

server: invalid /msg command

Finally, you don't need to worry that a client can no longer send a message "/msg" or "/join" to everyone, that will simply be a limitation.

Due Date

Lab 2 should be submitted by the start of class (12:50pm) on Tuesday, April 22nd.

You have 48 hours of late days for the three labs. They are consumed in 24 hour blocks and are used automatically. After they are used, you'll have the maximum grade you can receive for a late lab reduced by 25% each day.

Cabal -- Build & Test Tool

Cabal is the standard build and packaging tool for Haskell. A starting framework is provided for you. You can find the user guide for Cabal here.

Provided Files

The files provided to get started are:

If you add any new files, please make sure to add them into chat.cabal as otherwise they won't be packaged up when you run cabal sdist and then they won't be sumitted.

Building Lab 2

To get up and running (using Cabal), issue the following commands:

    cabal sandbox init

This will initiate a self-contained build environment where any dependencies you need are installed locally in the current directory. This helps avoid the Haskell equivalent of 'DLL Hell!'. If your version of cabal is older such that it doesn't have the sandbox command, then just proceed without it and it should all be fine.

Next, you want to build the lab. For that, issue the following commands:

    cabal install --only-dependencies --enable-tests
    cabal configure --enable-tests
    cabal build

After that, you should also be able to run the test harness simply by typing:

    cabal test

And you'll get some pretty output!

Testing Lab 2

Some skeleton code for a test framework is provided in TestHarness.hs. You'll need to edit it to add your own tests. The test framework uses a Haskell package called hspec. Please refer to it for documentation on how to use it.

We also strongly encourage the use of quick check! It can be integrated with the test driver that HSpec provides, please see the skeleton code that has an example of how.


While we strongly encourage you to take testing seriously and write a comprehensive test suite, we are only going to grade you on your glob matching implementation.

Grading will be just done on functionality but we will try to give feedback on your coding style.


First, simply type:

    cabal sdist

This will generate a tar file of your code in dist/globber.tar.gz.

Then go to and submit your work through the online form. You can resubmit as many times as you want up until the deadline.

If you have any trouble submitting on-line, then please email the staff mailing .