Package ej.hoka.tcp
Class TcpServer
- java.lang.Object
-
- ej.hoka.tcp.TcpServer
-
public class TcpServer extends Object
TCP/IP server that stores incoming connections.After starting this server with
start(), connections are available throughgetNextStreamConnection().
-
-
Constructor Summary
Constructors Constructor Description TcpServer(int port, int maxOpenedConnections)Constructs a new instance ofTcpServerusing environment's default socket factory.TcpServer(int port, int maxOpenedConnections, ServerSocketFactory serverSocketFactory)TcpServer(int port, int maxOpenedConnections, ServerSocketFactory serverSocketFactory, int timeout)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddConnection(Socket connection)Add a connection to the list of opened connections.InetAddressgetInetAddress()Gets the address to which this server is bound, if any.protected StringgetName()Returns the name of this TCPServer.SocketgetNextStreamConnection()Get the nextSocketto process.intgetPort()Gets the port number to which this server is listening to, if any.booleanisStopped()Returnstrueif theTcpServeris stopped.voidstart()Starts theTcpServer.voidstop()Stops theTcpServerand closes the connection.protected voidtooManyOpenConnections(Socket connection)Called when a connection cannot be added to the buffer.
-
-
-
Constructor Detail
-
TcpServer
public TcpServer(int port, int maxOpenedConnections)Constructs a new instance ofTcpServerusing environment's default socket factory.- Parameters:
port- the port to use.maxOpenedConnections- the maximal number of simultaneously opened connections.
-
TcpServer
public TcpServer(int port, int maxOpenedConnections, ServerSocketFactory serverSocketFactory)- Parameters:
port- the port to use.maxOpenedConnections- the maximal number of simultaneously opened connections.serverSocketFactory- theServerSocketFactory.
-
TcpServer
public TcpServer(int port, int maxOpenedConnections, ServerSocketFactory serverSocketFactory, int timeout)- Parameters:
port- the port to use.maxOpenedConnections- the maximal number of simultaneously opened connections.serverSocketFactory- theServerSocketFactory.timeout- the timeout of opened connections.- See Also:
Socket.setSoTimeout(int)
-
-
Method Detail
-
start
public void start() throws IOExceptionStarts theTcpServer. TheTcpServercan be started only once. Calling this method while theTcpServeris already running causes aIllegalStateException.- Throws:
IOException- if an error occurs during the creation of the socket.
-
stop
public void stop()
Stops theTcpServerand closes the connection.
-
addConnection
public void addConnection(Socket connection)
Add a connection to the list of opened connections.- Parameters:
connection-Socketto add
-
getNextStreamConnection
@Nullable public Socket getNextStreamConnection()
Get the nextSocketto process. Block until a new connection is available or server is stopped.- Returns:
- null if server is stopped
-
isStopped
public boolean isStopped()
Returnstrueif theTcpServeris stopped.- Returns:
trueif theTcpServeris stopped,falseotherwise
-
getName
protected String getName()
Returns the name of this TCPServer.- Returns:
- the string "TCPServer"
-
tooManyOpenConnections
protected void tooManyOpenConnections(Socket connection)
Called when a connection cannot be added to the buffer. By default, the connection is closed.- Parameters:
connection-Socketthat can not be added
-
getPort
public int getPort()
Gets the port number to which this server is listening to, if any.- Returns:
- the port number to which this server is listening or-1 if the socket is not bound yet.
-
getInetAddress
@Nullable public InetAddress getInetAddress()
Gets the address to which this server is bound, if any.- Returns:
- the address to which this server is bound,or null if the socket is unbound.
-
-