Java Reference
In-Depth Information
Program: Chat Client
This program is a simple chat program. You can't break in on ICQ or AIM with it, because
ing the server with the applet method
getCodeBase()
. The server for this will be presented
in
Chapter 16
. How does it look when you run it?
Figure 13-2
shows me chatting all by my-
self one day.
Figure 13-2. Chat client in action
The code is reasonably self-explanatory. We read from the remote server in a thread to make
the input and the output run without blocking each other; this is discussed in
Chapter 22
. The
reading and writing are discussed in this chapter. The program is shown in
Example 13-12
.
Example 13-12. ChatClient.java
public
public class
class
ChatClient
ChatClient
extends
extends
JFrame
{
private
private static
static final
final
long
long
serialVersionUID
= -
3686334002367908392L
;
private
private static
final
String userName
=
System
.
getProperty
(
"user.name"
,
"User With No Name"
);
/** The state of logged-in-ness */
protected
static final
protected
boolean
boolean
loggedIn
;
/* The main Frame. */
protected
protected
JFrame cp
;
/** The default port number */
protected
protected static
int
PORTNUM
=
ChatProtocol
.
PORTNUM
;
/** The actual port number */
protected
static final
final
int
protected
int
int
port
;
/** The network socket */
protected
protected
Socket sock
;