Description
Specification
Write a multi-threaded chat client program. Your chat client should get the user name and port number from the
command line. 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
NOTE: You will probably need to include additional options on the command line to compile and run JavaFX
programs. These examples are only intended to show command-line parameters used by the chat client program.
GUI
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.
Threads
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.
A (Very) Simple Chat Protocol
After connecting to the server (on port 4688) the client should send a message as follows:
connect UserName
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:
disconnect UserName
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.
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:
java ChatServer
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
Synchronization
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.
Notes
Network programming examples
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:
https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
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.
Updating the GUI from other threads
The GUI should only be updated from the JavaFX application thread. That means that the thread that receives
messages from the server cannot directly update the GUI. Instead of updating the GUI directly, it should use
Platform.runLater, which takes a Runnable object as a parameter and then runs the parameter on the
application thread.
Here is an example of using Platform.runLater from Chapter 32 of our textbook (page 32-10):
Platform.runLater(() -> lblText.setText(text));
This statement uses a lambda expression for the code that will be run on the application thread.
Turn in
Name the main class of your program ChatClient. Put your source file(s) into a zip file and turn the zip file in on
Canvas. Please do not put your classes in a package. Your file(s) should not be inside a folder in the zip file, and
there should not be any files other than source files and an optional README.txt file.
Points
10 Gets user name and port number from command line
10 Text area with vertical scroll bars, working disconnect button
10 Inner class that extends Thread or implements Runnable
10 Copies text field input to text area
15 Starts new thread to read from server
10 Text area is accessed only by synchronized code
15 Sends text field input to server
20 Receives messages from server and copies to text area
100 TOTAL
NOTE: If you don’t turn in source code you won’t receive credit for making an inner class, starting a new thread,
or synchronizing access to the text area.