context.arch.comm.protocol
Class TCPServerSocket

java.lang.Object
  |
  +--context.arch.comm.protocol.TCPServerSocket
Direct Known Subclasses:
HTTPServerSocket

public class TCPServerSocket
extends java.lang.Object
implements java.lang.Runnable, java.lang.Cloneable

This class implements a threaded serversocket that accepts TCP packets. It does nothing with the TCP packets themselves but can be subclassed to do real work. Whenever it receives data, it clones itself. The original class listens for more new connections while the clone handles the received data.

See Also:
Runnable, Cloneable

Field Summary
static int DEFAULT_PORT
          The default port number to use is 5555
 
Constructor Summary
TCPServerSocket()
          Default constructor for TCPServerSocket, with the default port.
TCPServerSocket(int port)
          TCPServerSocket constructor with user-specified port.
 
Method Summary
 void handleIncomingRequest(java.net.Socket data)
          This method handles data received on a given TCPServerSocket.
 void run()
          This method loops forever waiting for data on the socket.
 void start()
          Starts a ServerSocket and a thread with this TCPServerSocket as the Runnable.
 void stopServer()
          Stops the original thread (just the original?) running and closes the socket.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT

public static final int DEFAULT_PORT
The default port number to use is 5555
Constructor Detail

TCPServerSocket

public TCPServerSocket()
Default constructor for TCPServerSocket, with the default port.
See Also:
DEFAULT_PORT

TCPServerSocket

public TCPServerSocket(int port)
TCPServerSocket constructor with user-specified port.
Parameters:
port - Port number to use
Method Detail

start

public void start()
Starts a ServerSocket and a thread with this TCPServerSocket as the Runnable.

stopServer

public void stopServer()
Stops the original thread (just the original?) running and closes the socket.

run

public void run()
This method loops forever waiting for data on the socket. When data arrives, it clones a new instance of TCPServerSocket so the new thread can deal with the data, while the current instance looks for new data. The new thread deals with the data by calling handleIncomingRequest().
Specified by:
run in interface java.lang.Runnable
See Also:
handleIncomingRequest(java.net.Socket)

handleIncomingRequest

public void handleIncomingRequest(java.net.Socket data)
This method handles data received on a given TCPServerSocket. Could be abstract since the method does nothing. A subclass is expected to override this method to handle the incoming data as necessary.
Parameters:
data - Socket the data is arriving on