Package ej.hoka.tcp
Class TcpServer
- java.lang.Object
-
- ej.hoka.tcp.TcpServer
-
public class TcpServer extends java.lang.ObjectTCP/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, javax.net.ServerSocketFactory serverSocketFactory)TcpServer(int port, int maxOpenedConnections, javax.net.ServerSocketFactory serverSocketFactory, int timeout)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddConnection(java.net.Socket connection)Add a connection to the list of opened connections.java.net.InetAddressgetInetAddress()Gets the address to which this server is bound, if any.protected java.lang.StringgetName()Returns the name of this TCPServer.java.net.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(java.net.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, javax.net.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, javax.net.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 java.io.IOExceptionStarts theTcpServer. TheTcpServercan be started only once. Calling this method while theTcpServeris already running causes aIllegalStateException.- Throws:
java.io.IOException- if an error occurs during the creation of the socket.
-
stop
public void stop()
Stops theTcpServerand closes the connection.
-
addConnection
public void addConnection(java.net.Socket connection)
Add a connection to the list of opened connections.- Parameters:
connection-Socketto add
-
getNextStreamConnection
@Nullable public java.net.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 java.lang.String getName()
Returns the name of this TCPServer.- Returns:
- the string "TCPServer"
-
tooManyOpenConnections
protected void tooManyOpenConnections(java.net.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 java.net.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.
-
-