A ChatWindow supports two-way chatting between two users over the Internet. The constructor for a ChatWindow requires a connected Socket. Messages are sent and received through this socket. The window has a text-input box where the user can type messages. When the user presses return or clicks a “Send” button, the text in this box is transmitted over the connection. The window runs a thread which reads messages received from the other side of the connection.

Messages that are sent or received are displayed in a “transcript” that fills most of the window. There is a Close button that the user can click to close the connection and the window. Clicking in the window’s
close box has the same effect. The class includes a main program so it can be run as a stand-alone application. In this case, the program can act as a server that waits for a connection, or it can act as a client that connects to a ChatWindow server. After a connection is established, ChatWindows are opened by the client and by the server, and the users can chat with each other.

To run as a server listening on port 17171, use the command “java ChatWindow -s”. To listen on a different port, specify the port number as the second command-line argument. To run as a client. use “java ChatWindow ” where is the name or IP number of the computer on which the
program is running as a server. The listening port of the server can be specified as a second command-line parameter. Of course, ChatWindows can also be used by other programs. In particular, they are designed to work with the ConnectionBroker server and the BrokeredChat applet.
This program depends on the non-standard TextReader class, which is defined in TextReader.java.