public class TcpServer extends java.lang.Object implements RunnableWhichThrows<java.io.IOException>, Stoppable
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.
| Modifier and Type | Class and Description |
|---|---|
static interface |
TcpServer.ConnectionHandler
Represents a TCP connection to a remote peer (e.g. a server or a client).
|
| Constructor and Description |
|---|
TcpServer(java.net.InetSocketAddress endpoint,
int backlog,
javax.net.ssl.SSLContext sslContext,
TcpServer.ConnectionHandler clientConnectionHandler)
Creates a secure server with the specified endpoint and listen backlog.
|
TcpServer(java.net.InetSocketAddress endpoint,
int backlog,
TcpServer.ConnectionHandler clientConnectionHandler)
Creates a (non-secure) server with the specified endpoint and listen backlog.
|
| Modifier and Type | Method and Description |
|---|---|
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. |
public TcpServer(java.net.InetSocketAddress endpoint,
int backlog,
TcpServer.ConnectionHandler clientConnectionHandler)
throws java.io.IOException
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.
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 backlogjava.io.IOExceptionpublic TcpServer(java.net.InetSocketAddress endpoint,
int backlog,
javax.net.ssl.SSLContext sslContext,
TcpServer.ConnectionHandler clientConnectionHandler)
throws java.io.IOException
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.
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 backlogjava.io.IOExceptionpublic java.net.InetSocketAddress getEndpointAddress()
TcpServer(InetSocketAddress, int, ConnectionHandler)public void run()
throws java.io.IOException
RunnableWhichThrowsrun in interface RunnableWhichThrows<java.io.IOException>java.io.IOException