|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.net.tcp.Server
public abstract class Server
Fundamental building block for TCP servers.
This class implements the fundamental building blocks for creating a TCP service. It does most of the setup work required for creating a TCP server. You use this class in the following manner:
newConnection(java.net.Socket)
method to handle incoming client connections. Your implementation
should not block (you will most likely need to spawn off threads).
port number to listen for incoming
connection and any other properties for the object (the setProperties(com.ccg.util.TagLookup) method is handy for this).
start the server - it will call your
implementation of newConnection(java.net.Socket) each time a client
connects to your server.
The best way to see how to use this class is to look at the example program TimeServer.java which implements a simple server that sends the client information about the server's time and the client's connection.
| Field Summary | |
|---|---|
(package private) int |
_AcceptCount
Holds how many connections have been accepted. |
| Constructor Summary | |
|---|---|
Server()
Initializes object to its default state. |
|
| Method Summary | |
|---|---|
int |
getAcceptCount()
Get how many connections have been accepted. |
int |
getPort()
Get the TCP port number to listen for connections |
SocketOptions |
getSocketOptions()
Get the socket options to apply to all incoming connections |
boolean |
isShutdown()
Is server shutdown set? |
protected abstract void |
newConnection(Socket s)
What to do with each incoming connection. |
void |
setPort(int val)
Set the TCP port number to listen for connections |
Exception |
setProperties(TagLookup tl)
Set the value of the object from values specified in a TagLookup table. |
void |
setSocketOptions(SocketOptions val)
Set the socket options to apply to all incoming connections
This method allows one to specify a specific set of socket options (receive buffer size, send buffer
size, etc), which should be applied to all incoming connections. |
void |
start()
Starts the Server. |
void |
stop()
Attempt to gracefully shutdown the server. |
String |
toString()
Get the name/tag of the server. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
int _AcceptCount
connections have been accepted.
| Constructor Detail |
|---|
public Server()
After initializing a object using this method, one will want
to set the TCP port number to listen for
incoming connections and then start the server up
as a background thread. Once this is done, the start()
method will take care of accepting incoming TCP connections and
dispatch them to the newConnection(java.net.Socket) method which derived
classes will use to provide their clients with a service.
setPort(int),
start()| Method Detail |
|---|
protected abstract void newConnection(Socket s)
connection.
This method is invoked every time the server accepts an incoming connection. This method must be implemented by derived
classes. It's up to the derived class as to what to do with the
client's which connect (you should close the socket when you are
done with it).
The server will not accept anymore connections (clients) until this method returns. In almost all cases, implementations of this method will spawn off at least one separate thread to interact with the clients connection.
s - The Socket to use to communicate with the client.start()public void setSocketOptions(SocketOptions val)
socket options to apply to all incoming connections
This method allows one to specify a specific set of socket options (receive buffer size, send buffer
size, etc), which should be applied to all incoming connections. By default, this will be null indicating that no
special options should be applied.
val - New SocketOptions value to assign (pass null to leave
connections in the system default state).getSocketOptions()public SocketOptions getSocketOptions()
socket options to apply to all incoming connections
setSocketOptions(com.ccg.net.tcp.SocketOptions)public void setPort(int val)
val - New int value to assign.getPort()public int getPort()
setPort(int)public void stop()
public void start()
Server.
This method is used to start the Server. This will create
a background thread which will start listening for connections on
the specified port (the thread will "idle" if
you haven't specified a port yet). Every
incoming connection accepted will then trigger the invocation of
newConnection(java.net.Socket). Use stop() when you are ready to
shutdown the server.
stop()public boolean isShutdown()
public int getAcceptCount()
connections have been accepted.
start()public String toString()
toString in class Objectstart()public Exception setProperties(TagLookup tl)
TagLookup table.
This is a very flexible method to set the values contained in
this object based upon the values retrieved from the passed
TagLookup table (which can originate from about
any source - command line, applet parameters, property files,
etc). All values are optional - if not specified in the lookup
table, this object won't update its current settings for the
omitted fields. The following properties are recognized:
port=PORTSocketOptionsSocketOptions.setProperties(com.ccg.util.TagLookup) are
also recognized (receive buffer size, transmit buffer size,
etc.).
tl - The TagLookup table to fetch properties for.setPort(int),
SocketOptions.setProperties(com.ccg.util.TagLookup)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||