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.
- All communication is done using Unicode character streams (e.g.
java.io.Reader and java.io.Writer).
- When the client first connects, it sends "cmps109\nscreenName\n".
The "\n" is a newline character, not a backslash and an n.
- The cmps109 is used to let the server know that this is a proper client
trying to connect.
- The screenName is the name that will be used to
identify the user in the chat area.
- The first thing the server does is send a list of currently connected
client names (excluding the newly connected client)
to the newly connected client. This list has the following
format:
%
name1
name2
...
%
- Next, the server sends a message "[screenName]ENTERED THE ROOM"
to each currently connected client, including the newly connected client.
- After that, anything sent by the client is sent back out to all currently
connected clients (including the client that sent the posting). The message
sent by the server to the clients should contain the screenName of the
client making the posting in square brackets (e.g. [charlie]), followed
immediately by the actual posting. There should be no added space between
the close bracket and the message (e.g. [charlie]How are you?).
Each message is terminated with a newline character.
- The client should display the list of currently connected clients
either in the display window or in a separate window.
The initial list is given by the % delimited message
specified above.
If the client maintains a separate window of room occupants (not required)
the client needs to watch for enter and leave
messages to update the list. For this assignment you can assume that a
client never attempts to fake an enter or leave message.
- The clients display the messages as they are received from the server
(including messages that originated at the client displaying the message),
so that all clients display the messages in the same order.
- When the server detects that a connection has been broken, it should send
a message "[screenName]LEFT THE ROOM" to each currently connected client.
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
- 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),
- 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
- 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.
- 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]