3. Features
Allows a client to communicate
with all the other clients connected
to the server.
Light-weight.
Easy to operate !
Can add a chat room to your
website in minutes.
6. Protocol – Step 1
When a user types something into
their chat window, their message
will be sent as a string through a
DataOutputStream.
private void processMessage( String message )
{
try
{
// Send it to the server
dout.writeUTF( message );
// Clear out text input field
tf.setText( "" );
} catch( IOException ie ) {
System.out.println( ie ); }
}
7. Protocol – Step 2
When the server receives a message,
through a DataInputStream, it will send
this same
message to all users, again as a string
through a DataOutputStream.
void sendToAll( String message ) {
synchronized( outputStreams ) {
for (Enumeration e = getOutputStreams(); e.hasMoreElements(); )
{
DataOutputStream dout = (DataOutputStream)e.nextElement();
try {
dout.writeUTF( message );
} catch( IOException ie ) { System.out.println( ie ); }
}
}
}
8. Protocol – Step 3
The users will use a DataInputStream to
receive the message.
public void run() {
try {
while (true) {
String message = din.readUTF();
ta.append( message+"n" );
}
} catch( IOException ie ) { System.out.println( ie ); }
}