com.ccg.net.tcp
Class Server

java.lang.Object
  extended by com.ccg.net.tcp.Server

public abstract class Server
extends Object

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:

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.

Since:
1.0
Version:
$Revision: 1.3 $
Author:
$Author: pkb $
See Also:
TimeServer.java

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

_AcceptCount

int _AcceptCount
Holds how many connections have been accepted.

Since:
1.0
Constructor Detail

Server

public Server()
Initializes object to its default state.

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.

Since:
1.0
See Also:
setPort(int), start()
Method Detail

newConnection

protected abstract void newConnection(Socket s)
What to do with each incoming 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.

Parameters:
s - The Socket to use to communicate with the client.
Since:
1.0
See Also:
start()

setSocketOptions

public 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. By default, this will be null indicating that no special options should be applied.

Parameters:
val - New SocketOptions value to assign (pass null to leave connections in the system default state).
See Also:
getSocketOptions()

getSocketOptions

public SocketOptions getSocketOptions()
Get the socket options to apply to all incoming connections

Returns:
Current SocketOptions value assigned.
See Also:
setSocketOptions(com.ccg.net.tcp.SocketOptions)

setPort

public void setPort(int val)
Set the TCP port number to listen for connections

Parameters:
val - New int value to assign.
See Also:
getPort()

getPort

public int getPort()
Get the TCP port number to listen for connections

Returns:
Current int value assigned.
See Also:
setPort(int)

stop

public void stop()
Attempt to gracefully shutdown the server. This method will attempt to gracefully shutdown the server. This means that the server should stop accepting connections. see #isShutdown


start

public void start()
Starts the 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.

Since:
1.0
See Also:
stop()

isShutdown

public boolean isShutdown()
Is server shutdown set?

Returns:
Current boolean value assigned. see #setShutdown

getAcceptCount

public int getAcceptCount()
Get how many connections have been accepted.

Returns:
Total number of connections this object has accepted.
See Also:
start()

toString

public String toString()
Get the name/tag of the server.

Overrides:
toString in class Object
Returns:
Name/tag to associate with the server, "tcp" by default - used for logging messages and setting the name of the background thread.
Since:
1.0
See Also:
start()

setProperties

public Exception setProperties(TagLookup tl)
Set the value of the object from values specified in a 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=PORT
The TCP port to listen for incoming connections on.
SocketOptions
All of the options recognized by SocketOptions.setProperties(com.ccg.util.TagLookup) are also recognized (receive buffer size, transmit buffer size, etc.).

Parameters:
tl - The TagLookup table to fetch properties for.
Since:
1.0
See Also:
setPort(int), SocketOptions.setProperties(com.ccg.util.TagLookup)


Copyright 1998-1998-2006 null. All Rights Reserved.