This is a Java Application program that supports two way real time chatting or communication between two users over the Internet. The program can be run either as a standalone application or as a client-server application.

How the Java Chat Application works?

The program first checks if it is being run as a standalone application or as a client-server application. If it is being run as a standalone application, it establishes a connection between two programs, creates an object of type ChatWindow, and passes the connection to this object to handle communication. If it is being run as a client-server application, it waits for a connection as a server or connects to another program as a client.

The main class is ChatWindow which extends JFrame and implements ActionListener, WindowListener, and Runnable interfaces. 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 hits return key or clicks the “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/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.

How to Run the Server?

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.

ChatWindows can also be used by other applications in the system . 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.