Due: November 14
NOTE: I updated this specification to refer to our current textbook but the requirements have not changed. You can find the original specification here.
Write a multi-threaded chat client program.
Your chat client should get the user name and port number from the command
The user name should default to Anonymous and the port number
should default to 4688.
For example, this command line:
java ChatClient Frodo 4600
would use Frodo for the user name and connect to port 4600 on the server. The user should be able to specify a user name without a port number. In that case the program should use the default port number (4688):
java ChatClient Bilbo
Your program should have a GUI that contains a text area for the chat transcript and a text field for the user to enter new messages. The text area should have vertical scroll bars and should wrap lines at word breaks. Include a Disconnect button that sends the disconnect message to the server when the user clicks on it. You can optionally include a Send button that the user clicks when a message is ready to send.
Your program should have an inner class that extends Thread or implements Runnable. When your program starts it should create an instance of this class and start it running as a separate thread which waits for messages from the server and then copies each message to the text area.
After connecting to the server (on port 4688) the client should send
a message as follows:
to tell the server what name to add to the beginning of each message sent out from this client.
Messages are delimited by newline characters and have no special format. (This could be changed in the future if we make new versions of the protocol.)
When the server receives a message from a client, it adds the user name for that client to the beginning of the message and then sends the message to all clients except the client from which it received the message. That means that your client must add its own messages to the text area in addition to sending messages to the server.
A client sends the following message to disconnect:
The server will notify all other clients of the disconnection and will send the message disconnected back to the disconnecting client before closing the socket.
Your program will not receive full credit unless it works with the chat server in the chat2014.jar file.
Here's a version of the chat server that was compiled with Java 1.6,
in case you are using an older version of Java:
(The other version was compiled with Java 1.8.)
The chat2014.jar file is not executable so you have to remove the
files from the jar before running the server.
You can remove the files with the command:
jar xf chat2014.jar
You can run the server from the command line with the command:
To run client programs at the same time, you will need to run the server in the background on Unix-type systems or in a separate command window on Windows. You can run the client program in the jar file with the command:
java ChatClient UserName
Because the text area is accessed by more than one thread (the main thread and the thread that waits for messages from the server), it is a shared resource and access to it must be synchronized.
Figure 33.2 in our textbook (Introduction to Java) has a good summary of the variables and method calls involved in socket communication. (Note that Chapter 33 is an online chapter.)
The radius client and server in Chapter 33 is a good example of socket communication, but the transmitted data is doubles, not text, and the program is more complicated than necessary because both the client and the servers have GUIs.
For a simpler client and server example, take a look at the EchoClient
and EchoServer programs in the Java Tutorial:
These programs are simpler than the textbook's radius example because they don't have GUIs. Also, the messages they send are strings, as they are for our programs.
Name the main class of your program ChatClient. Put your source files into a jar or zip file and turn the jar or zip file in on Canvas. Please do not put your classes in a package. Your files should not be inside a folder in the jar file, and there should not be any files other than source files, class files, and an optional README.txt file.