de.unkrig.commons.net
Class TcpServer

java.lang.Object
  extended by de.unkrig.commons.net.TcpServer
All Implemented Interfaces:
RunnableWhichThrows<java.io.IOException>, Stoppable

public class TcpServer
extends java.lang.Object
implements RunnableWhichThrows<java.io.IOException>, Stoppable

A runnable that accepts TCP connections on a given interface/port and passes them to a TcpServer.ConnectionHandler. Supports SSL.

Notice that this implementation does not use java.nio.channels, i.e. connections are handled synchronously and each blocks one thread. In other words, it does not scale well for thousands of client connections; consider using NioTcpServer for that use case.


Nested Class Summary
static interface TcpServer.ConnectionHandler
          Represents a TCP connection to a remote peer (e.g. a server or a client).
 
Constructor Summary
TcpServer(java.net.InetSocketAddress endpoint, int backlog, java.io.File keyStoreFile, char[] keyStorePassword, java.lang.String serverAlias, TcpServer.ConnectionHandler clientConnectionHandler)
          Create a secure server with the specified endpoint and listen backlog.
TcpServer(java.net.InetSocketAddress endpoint, int backlog, TcpServer.ConnectionHandler clientConnectionHandler)
          Create a server with the specified endpoint and listen backlog.
 
Method Summary
 java.net.InetSocketAddress getEndpointAddress()
          Returns the address and port of the actual endpoint, which may differ from the endpoint given to TcpServer(InetSocketAddress, int, ConnectionHandler)
 void run()
          "To run" an instance means to call this method and wait until it returns.
 void stop()
          Returns when this Stoppable has completely stopped.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TcpServer

public TcpServer(java.net.InetSocketAddress endpoint,
                 int backlog,
                 TcpServer.ConnectionHandler clientConnectionHandler)
          throws java.io.IOException
Create a server with the specified endpoint and listen backlog.

The address of the endpoint can be used on a multi-homed host for a server that will only accept connect requests to one of its addresses. If address of the endpoint is the wildcard address, it will default accepting connections on any/all local addresses.

The port of the endpoint must be between 0 and 65535, inclusive. If it is zero, then an ephemoral port will be picked for the server socket.

The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed.

Parameters:
endpoint - The local port and InetAddress the server will bind to. If null, then the system will pick up an ephemeral port and a valid local address to bind the socket.
backlog - The listen backlog
Throws:
java.io.IOException

TcpServer

public TcpServer(java.net.InetSocketAddress endpoint,
                 int backlog,
                 @Nullable
                 java.io.File keyStoreFile,
                 @Nullable
                 char[] keyStorePassword,
                 @Nullable
                 java.lang.String serverAlias,
                 TcpServer.ConnectionHandler clientConnectionHandler)
          throws java.io.IOException,
                 java.security.KeyStoreException,
                 java.security.NoSuchAlgorithmException,
                 java.security.cert.CertificateException,
                 java.security.UnrecoverableKeyException,
                 java.security.KeyManagementException
Create a secure server with the specified endpoint and listen backlog.

The address of the endpoint can be used on a multi-homed host for a server that will only accept connect requests to one of its addresses. If address of the endpoint is the wildcard address, it will default accepting connections on any/all local addresses.

The port of the endpoint must be between 0 and 65535, inclusive. If it is zero, then an ephemoral port will be picked for the server socket.

The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed.

Parameters:
endpoint - The local port and InetAddress the server will bind to. If null, then the system will pick up an ephemeral port and a valid local address to bind the socket.
backlog - The listen backlog
Throws:
java.io.IOException
java.security.KeyStoreException
java.security.NoSuchAlgorithmException
java.security.cert.CertificateException
java.security.UnrecoverableKeyException
java.security.KeyManagementException
Method Detail

getEndpointAddress

public java.net.InetSocketAddress getEndpointAddress()
Returns the address and port of the actual endpoint, which may differ from the endpoint given to TcpServer(InetSocketAddress, int, ConnectionHandler)


run

public void run()
         throws java.io.IOException
Description copied from interface: RunnableWhichThrows
"To run" an instance means to call this method and wait until it returns.

Specified by:
run in interface RunnableWhichThrows<java.io.IOException>
Throws:
java.io.IOException

stop

public void stop()
Description copied from interface: Stoppable
Returns when this Stoppable has completely stopped.

Specified by:
stop in interface Stoppable