|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectsimpleNetworking.Server
public class Server
A simple Server that you can use in your programs. It has methods for: -- reading from each of the Clients attached to this Server -- writing to each of the Clients attached to this Server -- closing this Server's resources It does a minimal-security validation protocol with each Client who seeks to connect (the Client must use the Client's side of that protocol). You can construct this Server directly, but it is probably easier for you to: 1. Construct a new MultiServer. It constructs a Server for you. 2. Use the MultiServer's getServer(int n) method to get the Server after that Server has been connected to n validated Clients. For example, if you want a Server that communicates with 2 Clients, use: MultiServer multiServer = new MultiServer(); Server server = multiServer.getServer(2); ... code that uses ... server.readLine ... server.writeLine ... server.close ... as you see fit.
Field Summary | |
---|---|
(package private) static String |
PASSWORD
Password shared by the Server and all Clients in this minimal-security protocol. |
Constructor Summary | |
---|---|
Server()
Prepares to communicate with Clients, with no Clients accepted so far. |
|
Server(Socket socketToCommunicateWithClient)
Prepares to communicate with Clients, with the given Client as the sole Client accepted so far (assuming that the given Client passes the validation protocol). |
Method Summary | |
---|---|
void |
addClient(Socket socketToCommunicateWithClient)
Adds the given Client to the list of clients with which this Server can communicate, if the Client passes the validation protocol. |
void |
close()
For every Client currently associated with this Server, closes the resources associated with communicating with that Client. |
void |
close(int client)
Closes the resources associated with communicating to the given Client. |
int |
numberOfValidatedClients()
Returns the number of validated Clients currently associated with this Server. |
String |
readLine()
Convenience method intended for when there is a single Client. |
String |
readLine(int client)
Returns the next line available from this Server's nth Client, where n is the given parameter, stripping the terminating newline. |
String |
readLineIfReady()
Convenience method intended for when there is a single Client. |
String |
readLineIfReady(int client)
Returns the next line available from this Server's nth Client, where n is the given parameter, stripping the terminating newline. |
protected boolean |
validate(ReaderWriter connectionToClient)
To validate a proposed Client: -- The Client should send the (announced) password to this Server. |
void |
writeLine(String stringToWrite)
Convenience method intended for when there is a single Client. |
void |
writeLine(String stringToWrite,
int client)
Sends the given String, appending a newline to it, to this Server's nth Client, where n is the given parameter. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
static final String PASSWORD
Constructor Detail |
---|
public Server()
public Server(Socket socketToCommunicateWithClient) throws SignatureException, IOException
socketToCommunicateWithClient
- Socket to the sole Client with whom this Server can communicate so far
(assuming that the Client passes the validation protocol).
IOException
- if unable to construct a Reader/Writer to the Client.
SignatureException
- if unable to validate the given ClientMethod Detail |
---|
public void addClient(Socket socketToCommunicateWithClient) throws SignatureException, IOException
socketToCommunicateWithClient
- Socket to the Client to be added to the list of clients
with whom this Server can communicate,
if the Client passes the validation protocol.
IOException
- if unable to construct a Reader/Writer to the Client.
SignatureException
- if unable to validate the given Clientpublic String readLine(int client) throws IOException, IndexOutOfBoundsException
client
- Index in the array of Clients for this Server,
indicating which Client from which to read.
IOException
- if an IO error occurs while reading.
IndexOutOfBoundsException
- if the given index of the Client is out of bounds.public String readLine() throws IOException
IOException
- if an IO error occurs while reading.public String readLineIfReady(int client) throws IOException, IndexOutOfBoundsException
client
- Index in the array of Clients for this Server,
indicating which Client from which to read.
IOException
- if an IO error occurs while reading.
IndexOutOfBoundsException
- if the given index of the Client is out of bounds.public String readLineIfReady() throws IOException
IOException
- if an IO error occurs while reading.public void writeLine(String stringToWrite, int client) throws IOException, IndexOutOfBoundsException
stringToWrite
- String to send (with a newline appended)
to this Server's nth Client.client
- Index in the array of Clients for this Server,
indicating which Client to which to write.
IOException
- if an error occurs while writing.
IndexOutOfBoundsException
- if the given index of the Client is out of bounds.public void writeLine(String stringToWrite) throws IOException
stringToWrite
- String to send (with a newline appended)
to this Server's first (and presumably only) Client.
IOException
- if an IO error occurs while reading.protected boolean validate(ReaderWriter connectionToClient)
connectionToClient
- ReaderWriter to Client to use for validation.
public void close(int client)
client
- Index in the array of Clients for this Server,
indicating which Client to close resources for communicating.public void close()
public int numberOfValidatedClients()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |