{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ConnectionWindow - A ConnectionWindow supports two-way...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/* A ConnectionWindow supports two-way chatting between two users over the Internet. The constructor for a ConnectionWindow 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 ConnectionWindow server. After a connection is established, ConnectionWindows are opened by the client and by the server, and the users can chat with each other. To run as a server listing on port 17171, use the command "java ConnectionWindow -s". To listen on a different port, specify the port number as the second command-line argument. To run as a client. use "java ConnectionWindow <server>" where <server> 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, ConnectionWindows can also be used by other programs. In particular, they are designed to work with the ConnectionBroker server and the BrokeredChat applet. */ * import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; i public class ConnectionWindow extends Frame implements ActionListener, WindowListener, Runnable { /* The "static" parts of this class are used when it is run as a standalone application. In that case, it makes a connection, either as a server or client, then creates an object of type ConnectionWindow to handle the connection. */ static final String HANDSHAKE = "<Independent Connection Window>"; // The HANDSHAKE string is sent by each end of the connection // to the other end, as soon as the connection is opened. // This is done to verify that the partner in the connection // is another instance of this program. (This is used in // the main() routine.) static final int DEFAULT_PORT = 17171; // Port on which the // server listens, if // none is specified // on the command line. public static final void main(String[] args) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
int port; // Port on which the server listens. Socket connection; // For communication with the // program on the other end. TextReader in; // A stream for receiving the handshake. PrintWriter out; // A stream for sending the handshake.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}