Package coffeedraw
Class Client
- java.lang.Object
-
- coffeedraw.Client
-
- All Implemented Interfaces:
java.lang.Runnable
public class Client extends java.lang.ObjectAClientmakes a connection to another computer (the server) and can send messages to the server and receive messages back. You will never create aClientdirectly. Instead, create an object that implements theClientListenerinterface, and pass that object toNetwork.configure()so that it will be notified when the user has connected to a server and when messages come in from that server.
TheClientobject is passed to all theClientListenermethods. You can use that object to send a reply to the server right away, using thesendMessage()method, or you can keep theClientin an instance variable so that you can send messages later when you need to. TheClientalso has methods to determine the name the user entered for theirClient, and the name and address of theServerthat they joined.- Author:
- Noah Coetsee (noahcoder77@gmail.com)
- See Also:
ClientListener,Network.configure(String, ServerListener, ClientListener)
-
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.StringgetName()Get the client name.java.net.InetSocketAddressgetServerAddress()Get the server address.java.lang.StringgetServerName()Get the server name.booleanisRunning()Check if the client is still connected.voidrun()Deprecated.for internal use onlyvoidsendMessage(java.lang.String message)Send a message to the server.voidshutDown()Shut down the client, disconnecting it from the server.
-
-
-
Method Detail
-
sendMessage
public void sendMessage(java.lang.String message)
Send a message to the server. This will result in the server listener getting amessageReceived()event.- Parameters:
message- The message to send.
-
getName
public java.lang.String getName()
Get the client name. This is the name that the user entered in the "Join network game" window.- Returns:
- the client name
-
getServerName
public java.lang.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
-
getServerAddress
public java.net.InetSocketAddress getServerAddress()
Get the server address.- Returns:
- the server address
-
isRunning
public boolean isRunning()
Check if the client is still connected.- Returns:
trueif the client is running,falseotherwise.
-
shutDown
public void shutDown()
Shut down the client, disconnecting it from the server. The client listener will receive aclientDisconnected()message, and the server listener will receive achannelClosed()event. Quitting the program will automatically shut down the client.
-
run
public void run()
Deprecated.for internal use only- Specified by:
runin interfacejava.lang.Runnable
-
-