Class ChatClient
This class implements the chat client, as described. This involves setting up a basic user interface, handling user interaction, and receiving messages from the server.
The
ChatClient
class extends Frame
; this is typical for a graphical application. We implement the Runnable
interface so that we can start a Thread
that receives messages from the server. The constructor performs the basic setup of the GUI, the run()
method receives messages from the server, the handleEvent()
method handles user interaction, and the main()
method performs the initial network connection.
The constructor takes three parameters: a title for the window, an input stream, and an output stream. The
ChatClient
communicates over the specified streams; we create buffered data streams i and o to provide efficient higher-level communication facilities over these streams. We then set up our simple user interface, consisting of theTextArea
output and the TextField
input. We layout and show the window, and start a Thread
listener that accepts messages from the server.
When the listener thread enters the run method, we sit in an infinite loop reading
String
s from the input stream. When a String
arrives, we append it to the output region and repeat the loop. An IOException
could occur if the connection to the server has been lost. In that event, we print out the exception and perform cleanup. Note that this will be signalled by an EOFException
from thereadUTF()
method.
To clean up, we first assign our listener reference to this
Thread
to null
; this indicates to the rest of the code that the thread has terminated. We then hide the input field and call validate()
so that the interface is laid out again, and close the OutputStream
o to ensure that the connection is closed.
Note that we perform all of the cleanup in a
finally
clause, so this will occur whether an IOException
occurs here or the thread is forcibly stopped. We don't close the window immediately; the assumption is that the user may want to read the session even after the connection has been lost.
In the
handleEvent()
method, we need to check for two significant UI events:
The first is an action event in the
TextField
, which means that the user has hit the Return key. When we catch this event, we write the message to the output stream, then call flush()
to ensure that it is sent immediately. The output stream is a DataOutputStream
, so we can use writeUTF()
to send a String
. If an IOException
occurs the connection must have failed, so we stop the listener thread; this will automatically perform all necessary cleanup.
The second event is the user attempting to close the window. It is up to the programmer to take care of this task; we stop the listener thread and hide the
Frame
.
The
main()
method starts the client; we ensure that the correct number of arguments have been supplied, we open aSocket
to the specified host and port, and we create a ChatClient
connected to the socket's streams. Creating the socket may throw an exception that will exit this method and be displayed.
No comments:
Post a Comment