java.lang.Objectobjectdraw.Server
public class Server
A Server
runs on one computer and allows other computers (clients)
to connect to it. The clients can then send messages to the server and receive
messages back. The server also announces its presence across the local area
network (LAN) so that computers at the same site can see that a game is happening.
You will never create a Server
object directly. Instead, write a
class that implements ServerListener
and pass an object of that
class to Network.configure()
. This will result in that object
being informed when server-related events happen.
The Server
class contains methods to send messages either to a
particular client, or to all the clients at once. Your ServerListener
will be passed a Server
object with each event; you could also keep
it in an instance variable so that you can send messages whenever you want.
ServerListener
,
Network.configure(String, ServerListener, ClientListener)
Method Summary | |
---|---|
String |
getName()
Get the server name. |
boolean |
isRunning()
Check if the server is still running. |
void |
sendMessage(String message)
Send a message to every client connected to the server. |
void |
sendMessage(String message,
int channelID)
Send a message to a single client. |
void |
shutDown()
Shut down the server, disconnecting all clients. |
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 |
---|
public String getName()
public boolean isRunning()
true
if the server is running, false
otherwise.public void sendMessage(String message)
messageReceived()
event on
all the computers connected to this server.
message
- The message to send.public void sendMessage(String message, int channelID)
messageReceived()
event. The channel
ID is the number identifying that client that is passed to the listener along
with client-specific events.
message
- The message to send.channelID
- The ID of the client to send the message to.public void shutDown()
clientDisconnected()
message, and the server
listener will receive a channelClosed()
event for every client,
followed by a serverStopped()
event. Quitting the program will
automatically shut down the server.