net.mtu.eggplant.util.network
Class TCPServer

java.lang.Object
  extended by net.mtu.eggplant.util.network.TCPServer
All Implemented Interfaces:
Cloneable, Runnable

public class TCPServer
extends Object
implements Runnable, Cloneable

Simple TCP echo server.

Version:
$Revision$

Field Summary
static int DEFAULT_PORT
           
 
Constructor Summary
TCPServer()
          default constructor.
TCPServer(int port)
          Create a ServerSocket to listen for connections on If port < 0 or port > 65535 port defaults to DEFAULT_PORT.
 
Method Summary
protected  Socket getSocket()
          get the actual socket
 void initializeConnection()
          Actually make the connection and wait for data.
static void main(String[] args)
           
 void print(String message)
          print something to the socket.
protected  boolean processData(String line)
          do something with a line of input.
 String readLine()
          read a line from the socket
 void run()
          The body of the server thread.
protected  void setSocket(Socket s)
          set the actual 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
See Also:
Constant Field Values
Constructor Detail

TCPServer

public TCPServer()
default constructor. Uses port 6789 as the port to listen on.


TCPServer

public TCPServer(int port)
Create a ServerSocket to listen for connections on If port < 0 or port > 65535 port defaults to DEFAULT_PORT.

Parameters:
port - the port to listen on
Method Detail

run

public void run()
The body of the server thread. Loop forever, listening for and accepting connections from clients. For each connection, create a clone that executes initializeConnection().

Specified by:
run in interface Runnable

initializeConnection

public void initializeConnection()
Actually make the connection and wait for data. Just echo data back on the socket.


processData

protected boolean processData(String line)
do something with a line of input. Just print the line back out on the socket. Override this to do more with the data.

Parameters:
line - the input
Returns:
true if we should quit, false otherwise

print

public void print(String message)
print something to the socket.

Parameters:
message - the message, if not connected nothing happens

readLine

public String readLine()
                throws IOException
read a line from the socket

Returns:
the line, null if not connected
Throws:
IOException

getSocket

protected Socket getSocket()
get the actual socket

Returns:
the socket, null if not connected

setSocket

protected void setSocket(Socket s)
set the actual socket.

Parameters:
s - the socket

main

public static void main(String[] args)