public class TcpServer extends Object
After starting this server with start()
, connections are available through
getNextStreamConnection()
.
Constructor and Description |
---|
TcpServer(int port,
int maxOpenedConnections)
Constructs a new instance of
TcpServer using environment's default socket factory. |
TcpServer(int port,
int maxOpenedConnections,
ServerSocketFactory serverSocketFactory)
|
TcpServer(int port,
int maxOpenedConnections,
ServerSocketFactory serverSocketFactory,
int timeout)
|
Modifier and Type | Method and Description |
---|---|
void |
addConnection(Socket connection)
Add a connection to the list of opened connections.
|
InetAddress |
getInetAddress()
Gets the address to which this server is bound, if any.
|
protected String |
getName()
Returns the name of this TCPServer.
|
Socket |
getNextStreamConnection()
Get the next
Socket to process. |
int |
getPort()
Gets the port number to which this server is listening to, if any.
|
boolean |
isStopped()
Returns
true if the TcpServer is stopped. |
void |
start()
Starts the
TcpServer . |
void |
stop()
Stops the
TcpServer and closes the connection. |
protected void |
tooManyOpenConnections(Socket connection)
Called when a connection cannot be added to the buffer.
|
public TcpServer(int port, int maxOpenedConnections)
TcpServer
using environment's default socket factory.port
- the port to use.maxOpenedConnections
- the maximal number of simultaneously opened connections.public TcpServer(int port, int maxOpenedConnections, ServerSocketFactory serverSocketFactory)
port
- the port to use.maxOpenedConnections
- the maximal number of simultaneously opened connections.serverSocketFactory
- the ServerSocketFactory
.public TcpServer(int port, int maxOpenedConnections, ServerSocketFactory serverSocketFactory, int timeout)
port
- the port to use.maxOpenedConnections
- the maximal number of simultaneously opened connections.serverSocketFactory
- the ServerSocketFactory
.timeout
- the timeout of opened connections.Socket.setSoTimeout(int)
public void addConnection(Socket connection)
connection
- Socket
to add@Nullable public InetAddress getInetAddress()
protected String getName()
@Nullable public Socket getNextStreamConnection()
Socket
to process. Block until a new connection is available or server is stopped.public int getPort()
public boolean isStopped()
true
if the TcpServer
is stopped.true
if the TcpServer
is stopped, false
otherwisepublic void start() throws IOException
TcpServer
. The TcpServer
can be started only once. Calling this method while the
TcpServer
is already running causes a IllegalStateException
.IOException
- if an error occurs during the creation of the socket.public void stop()
TcpServer
and closes the connection.