public class Server
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_PORT
_more_
|
Constructor and Description |
---|
Server()
Create a new Server connection.
|
Server(int port)
Create a new Server connection on the given port.
|
Modifier and Type | Method and Description |
---|---|
void |
addClient(Client client)
Add the given client to the list of clients managed by this server.
|
void |
addClient(Client client,
boolean andStartListening)
Add the given client to the list of clients managed by this server.
|
protected Client |
createClient(java.net.Socket clientSocket)
A factory method for creating a new client.
|
java.util.List |
getClients()
Gets the clients.
|
boolean |
getIsRunning()
Get the IsRunning property.
|
protected void |
handleIncomingMessage(Client fromClient,
java.lang.String message)
Handle the message rcvd from the given client.
|
boolean |
hasClients()
Does this server have any clients.
|
protected void |
initClient(java.net.Socket clientSocket)
Create a new client object and listen for input.
|
protected void |
logException(java.lang.String msg,
java.lang.Exception exc)
Log exception.
|
static void |
main(java.lang.String[] args)
The main method.
|
protected void |
notifyClientAdd(Client client)
Gets called when we have added a new client.
|
protected void |
notifyClientRemove(Client client)
Gets called when we have removed a client.
|
protected void |
notifyServerStart()
Gets called when we are starting to listen
|
protected void |
notifyServerStop()
Gets called when we are done running on the server socket
|
void |
removeClient(Client client)
Remove the given client from the list of clients managed by this server.
|
void |
startServer()
Start server.
|
void |
startServer(int newPort)
Start server.
|
void |
stopServer()
Stop server.
|
void |
write(java.lang.String message)
Write.
|
void |
write(java.lang.String message,
Client exceptClient)
Write.
|
public Server()
public Server(int port)
port
- the portpublic void startServer(int newPort) throws java.io.IOException
newPort
- the new portjava.io.IOException
- Signals that an I/O exception has occurred.public void startServer() throws java.io.IOException
java.io.IOException
- Signals that an I/O exception has occurred.public void stopServer()
protected void notifyServerStart()
protected void notifyServerStop()
protected void notifyClientAdd(Client client)
client
- the clientprotected void notifyClientRemove(Client client)
client
- the clientpublic boolean getIsRunning()
protected Client createClient(java.net.Socket clientSocket) throws java.io.IOException
clientSocket
- The socket we are connected to the client with.java.io.IOException
- Signals that an I/O exception has occurred.protected void initClient(java.net.Socket clientSocket)
clientSocket
- the client socketpublic java.util.List getClients()
public boolean hasClients()
public void addClient(Client client)
client
- the clientpublic void addClient(Client client, boolean andStartListening)
client
- the clientandStartListening
- the and start listeningpublic void removeClient(Client client)
client
- the clientprotected void handleIncomingMessage(Client fromClient, java.lang.String message)
fromClient
- the from clientmessage
- the messagepublic void write(java.lang.String message)
message
- the messagepublic void write(java.lang.String message, Client exceptClient)
message
- the messageexceptClient
- the except clientprotected void logException(java.lang.String msg, java.lang.Exception exc)
msg
- the msgexc
- the excpublic static void main(java.lang.String[] args)
args
- the arguments