public class TcpServer extends Object implements de.unkrig.commons.lang.protocol.RunnableWhichThrows<IOException>, de.unkrig.commons.lang.protocol.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(InetSocketAddress endpoint,
int backlog,
SSLContext sslContext,
TcpServer.ConnectionHandler clientConnectionHandler)
Creates a secure server with the specified endpoint and listen backlog.
|
TcpServer(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 |
|---|---|
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()
Accepts connection requests from clients until the end of time.
|
void |
stop() |
public TcpServer(InetSocketAddress endpoint, int backlog, TcpServer.ConnectionHandler clientConnectionHandler) throws 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 ephemeral 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 (as of Java 6, 7 and 8: 50) will be assumed.
endpoint - The InetAddress and local port the server will bind to; InetSocketAddress.InetSocketAddress(int) will accept connections on any/all
local addresses; port number zero will pick an ephemeral portbacklog - The listen backlogclientConnectionHandler - Is called exactly once for each accepted connectionIOExceptionpublic TcpServer(InetSocketAddress endpoint, int backlog, SSLContext sslContext, TcpServer.ConnectionHandler clientConnectionHandler) throws 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 backlogclientConnectionHandler - Is called exactly once for each accepted connectionIOExceptionpublic InetSocketAddress getEndpointAddress()
TcpServer(InetSocketAddress, int, ConnectionHandler)public void run()
throws IOException
clientConnectionHandler.
When that returns, it closes the client socket and the thread dies.
This method completes normally only when stop() is invoked by another thread.
run in interface de.unkrig.commons.lang.protocol.RunnableWhichThrows<IOException>IOExceptionpublic void stop()
stop in interface de.unkrig.commons.lang.protocol.StoppableCopyright © 2019 Arno Unkrig. All rights reserved.