Homework 8 - Threads and Sockets: A chat room


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

Reading: Chapter 13.

Due: At 8am, March 15, 2002 submit is turned off automatically. Work turned in after that time will not be accepted for grading.

Purpose

The purpose of this assignment is to become familiar with creating threads and using sockets. This is an example of client/server computing.

Program Description

For this assignment you will create two programs that will function like an Internet chat server and client. One program will begin listening for incoming "calls" using accept(). The other program will attempt to connect to some already started server. It is possible but not required that all of this functionality be included in a single program.

For full credit your client program will need to have a graphical user interface that contains at least two text areas. One text area should be used to allow the user to compose a message to post to the chat area (e.g. a JTextField). The other text area should be used to display what each currently connected user posts, identified with the users screen name (e.g. a JTextArea).

For the program that starts first (the server or initial listener that does the accept() call), you must allow for the user to specify the port number, either on the command line or through some GUI component.

For the program that starts second (the client), you must allow for the user to specify both the port number and the remote hostname/address. Again, this can be done either on the command line or via a GUI component.

You program must adhere to the following very simple protocol.

How to proceed

You can start with MiniServer and MiniClient. The biggest difference between these programs and your program is that they contain only one thread each. Each thread is either listening to the local console (Console.in.readLine()) or listening to the network (input.readLine()). For this assignment you will need more than one thread in both the server and the client.

In the client you will need

  1. one thread that responds to user actions (e.g. clicking a Send button), and sends messages to the server (you can just use the AWT event thread for this),
  2. and another thread the listens to the socket and sends lines to the chat display area of the client (you can use the main thread for this).

In the server you will need

  1. one thread for each connected client. These threads listen for incoming messages from the associated client, and then send these messages to some object on the server that is responsible for sending the newly received message out to each of the clients.
  2. The server also needs a thread to listen for new client connections.

Sample Solution

I also have a sample solution (not heavily tested) available. To run the server type:
         java hw6.MultiServer portnum
To run the client type:
         java hw6.GUIClient hostname portnum screenName
You can copy this to your own machine. The files are in ~mcdowell/java/public/hw6.jar. After copying the file type:
         jar xf hw6.jar
Then you can type the same commands as above. On your own computer you can start several command windows and use "localhost" as the host name.

Doing your own work

As a final reminder. The program you submit must be your own original work. If you use code snippets from other example programs you find in text books etc. you MUST give them credit by clearlly indicating in your comments where the code came from. I'm sure there are complete Java chat programs out there in the Internet somewhere. You should not use significant portions of other existing programs (beyond MiniClient and MiniServer). If you find a code snippet you would like to use and your have any questions about whether it is appopriate, don't hesitate to ask me (Charlie McDowell).
[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]