objectdraw
Class Client

java.lang.Object
  extended by objectdraw.Client
All Implemented Interfaces:
Runnable

public class Client
extends Object

A Client makes a connection to another computer (the server) and can send messages to the server and receive messages back. You will never create a Client directly. Instead, create an object that implements the ClientListener interface, and pass that object to Network.configure() so that it will be notified when the user has connected to a server and when messages come in from that server.

The Client object is passed to all the ClientListener methods. You can use that object to send a reply to the server right away, using the sendMessage() method, or you can keep the Client in an instance variable so that you can send messages later when you need to. The Client also has methods to determine the name the user entered for their Client, and the name and address of the Server that they joined.

Author:
Russell Zahniser (russell@zahniser.net)
See Also:
ClientListener, Network.configure(String, ServerListener, ClientListener)

Method Summary
 String getName()
          Get the client name.
 InetSocketAddress getServerAddress()
          Get the server address.
 String getServerName()
          Get the server name.
 boolean isRunning()
          Check if the client is still connected.
 void sendMessage(String message)
          Send a message to the server.
 void shutDown()
          Shut down the client, disconnecting it from the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Method Detail

getName

public String getName()
Get the client name. This is the name that the user entered in the "Join network game" window.

Returns:
the client name

getServerAddress

public InetSocketAddress getServerAddress()
Get the server address.

Returns:
the server address

getServerName

public String getServerName()
Get the server name. This is the name that the user selected from the list in the "Join network game" window.

Returns:
the server name

isRunning

public boolean isRunning()
Check if the client is still connected.

Returns:
true if the client is running, false otherwise.

sendMessage

public void sendMessage(String message)
Send a message to the server. This will result in the server listener getting a messageReceived() event.

Parameters:
message - The message to send.

shutDown

public void shutDown()
Shut down the client, disconnecting it from the server. The client listener will receive a clientDisconnected() message, and the server listener will receive a channelClosed() event. Quitting the program will automatically shut down the client.