Simple Chat Room Assignment

Table of Contents

What is a chat room?

You hopefully have some idea of what a chat room is. There are lots of different kinds, but for this assignment's purposes, a chat room is a program (or collection of programs) that does the following: (Think ICQ, Instant messenger, Zwrite, ytalk, etc.) Here's a picture:

[Chat Room Screen Shot]
You type into the entry line at the bottom, hit Enter, and everyone else in the chat room gets your message. The server sends a message whenever anyone logs on.


For this assignment, you will be given a working version of the chat room program in the form of Java .class files. Your job will be to replace the executables with your own, one by one. This saves you from having to simultaneously debug a client and a server. A reference for the given classes is available. You do not have to use the same class structure, package structure, or design as the given code.

Message protocol

For this assignment, chat messages will have the following format:

field name size/encoding content
n byte length of name in bytes
name byte[n] name of person sending message
m int (4 bytes, network byte order) length of text in bytes
text byte[m] text of message

(The fields are sent top first, bottom last.)

The idea behind this standard is that everyone's chat room should end up being compatible. That is, every client should work with every server.

The protocol is stateless, and clients are not expected to be able to show messages that were sent before they connected to the server. The server will discard messages once they are sent, and there is no way to get them back.

The client and server don't exchange any information other than these chat messages. That is, the client doesn't need to send any requests to the server, and the server will send no acknowledgment. You can tell when the connection has failed because you'll get an IOException or something like that the next time you try to read or write a message. The client needs to watch for this in case the server goes down, and the server needs to watch for this because clients are allowed to disconnect at any time.

Given code

Since this is a networking course, we've given you the source for the GUI for free in the file (included with the assignment download). You are free to use or modify it, or not to use it at all. Please include it with your final submission if you use it.

Running the sample solution

The sample solution and the GUI source code can be downloaded here. The code is already built, but you may rebuild it by typing javac *.java. To test the chat code, type
   java ThreadedServer
on one host and
   java SClient Nickname servername
on another. Both hosts may be the same, in which case you should use localhost for the hostname, or just leave it blank.

The monitor client

Write a monitor client. It should contact a chat server and display all messages that come from it. It shouldn't send any messages, just monitor the ones that get sent. This program is useful for debugging and should be easy to write.

The server

Write a chat room server that uses the ServerSocket and Socket classes in package These classes use TCP/IP streams. When it receives a message from a client, it should send a copy of it to all its clients (including the one it came from). Clients should be able to connect to and disconnect from the server at will. When a client connects, the server should broadcast a message from "server" stating that someone has logged in, and give their host and IP address. (There is no way to give their name at this point. That comes in a later assignment on authentication.)

The GUI client

Write a fully functional client with a graphical user interface something like the screen shot above. There should be at minimum a text field that shows all the messages that have been broadcast so far (including who sent them and the text) and a text field where you can type a message and send it. You should be able to specify a name that will be sent with your messages, either with a command line option, a system property, a configuration file, or with a dialog box that pops up. You should display this name somewhere on the screen.

Note that the client really needs to be multi-threaded, but Java happens to do most of the work for you. One thread waits for messages to come down the network connection, while the other thread handles window events, key strokes, and sending messages. In Java, the AWT stuff automatically runs in its own thread, so you can use the main thread to do all your reading.

Other Requirements

The host and port where the clients look for the server should not be hard coded. It's okay to put in a default value, but it should be possible to change the server host and port either with a command line option, a system property, a dialog box, a configuration file, something like that. Be sure to document how this works in your README file.

The clients and server must be able to run on any machine and any port.