Package ej.hoka.tcp

Class TcpServer


  • public class TcpServer
    extends java.lang.Object
    TCP/IP server that stores incoming connections.

    After starting this server with start(), connections are available through getNextStreamConnection().

    • Constructor Summary

      Constructors 
      Constructor Description
      TcpServer​(int port, int maxOpenedConnections)
      Constructs a new instance of TcpServer using environment's default socket factory.
      TcpServer​(int port, int maxOpenedConnections, javax.net.ServerSocketFactory serverSocketFactory)
      Constructs a new instance of TcpServer using the ServerSocketFactory serverSocketFactory.
      TcpServer​(int port, int maxOpenedConnections, javax.net.ServerSocketFactory serverSocketFactory, int timeout)
      Constructs a new instance of TcpServer using the ServerSocketFactory serverSocketFactory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addConnection​(java.net.Socket connection)
      Add a connection to the list of opened connections.
      java.net.InetAddress getInetAddress()
      Gets the address to which this server is bound, if any.
      protected java.lang.String getName()
      Returns the name of this TCPServer.
      java.net.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​(java.net.Socket connection)
      Called when a connection cannot be added to the buffer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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,
                         javax.net.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,
                         javax.net.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

      • start

        public void start()
                   throws java.io.IOException
        Starts the TcpServer. The TcpServer can be started only once. Calling this method while the TcpServer is already running causes a IllegalStateException.
        Throws:
        java.io.IOException - if an error occurs during the creation of the socket.
      • stop

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

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

        @Nullable
        public java.net.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 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 - 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 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.