Package ej.hoka.tcp

Class TcpServer

    • Constructor Detail

      • TcpServer

        public TcpServer​(int port,
                         int maxOpenedConnections)
        Constructs a new instance of TcpServer using 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)
        Constructs a new instance of TcpServer using the ServerSocketFactory serverSocketFactory.
        Parameters:
        port - the port to use.
        maxOpenedConnections - the maximal number of simultaneously opened connections.
        serverSocketFactory - the ServerSocketFactory.
      • TcpServer

        public TcpServer​(int port,
                         int maxOpenedConnections,
                         ServerSocketFactory serverSocketFactory,
                         int timeout)
        Constructs a new instance of TcpServer using the ServerSocketFactory serverSocketFactory.
        Parameters:
        port - the port to use.
        maxOpenedConnections - the maximal number of simultaneously opened connections.
        serverSocketFactory - the ServerSocketFactory.
        timeout - the timeout of opened connections.
        See Also:
        Socket.setSoTimeout(int)
    • Method Detail

      • stop

        public void stop()
        Stops the TcpServer and closes the connection.
      • addConnection

        public void addConnection​(Socket connection)
        Add a connection to the list of opened connections.
        Parameters:
        connection - Socket to add
      • getNextStreamConnection

        @Nullable
        public Socket getNextStreamConnection()
        Get the next Socket to process. Block until a new connection is available or server is stopped.
        Returns:
        null if server is stopped
      • isStopped

        public boolean isStopped()
        Returns true if the TcpServer is stopped.
        Returns:
        true if the TcpServer is stopped, false otherwise
      • 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 - Socket that 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.