Class TcpServer

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, ConnectionListener

    public class TcpServer
    extends TcpBaseServer
    Class that manages allows to send and receive messages via TCP sockets. Uses 1 thread/connection to read messages.
    Author:
    Vladimir Blagojevic, Bela Ban
    • Field Detail

      • srv_sock

        protected java.net.ServerSocket srv_sock
      • acceptor

        protected java.lang.Thread acceptor
      • buffered_inputstream_size

        protected int buffered_inputstream_size
      • buffered_outputstream_size

        protected int buffered_outputstream_size
      • log_accept_error

        protected boolean log_accept_error
    • Constructor Detail

      • TcpServer

        public TcpServer​(java.net.InetAddress bind_addr,
                         int port)
                  throws java.lang.Exception
        Creates an instance of TcpServer that creates a server socket and listens for connections. The end port defaults to (port + 50). Needs to be started next.
        Parameters:
        bind_addr - The local address to bind to. If null, the address will be picked by the OS
        port - The local port to bind to. If 0, the port will be picked by the OS.
        Throws:
        java.lang.Exception - Thrown if the creation failed
      • TcpServer

        public TcpServer​(IpAddress bind_addr)
                  throws java.lang.Exception
        Creates an instance of TcpServer.
        Parameters:
        bind_addr - The local bind address and port. If null, a bind address and port will be picked by the OS.
        Throws:
        java.lang.Exception
      • TcpServer

        public TcpServer​(ThreadFactory thread_factory,
                         SocketFactory socket_factory,
                         java.net.InetAddress bind_addr,
                         int srv_port,
                         int end_port,
                         java.net.InetAddress external_addr,
                         int external_port)
                  throws java.lang.Exception
        Creates an instance of TcpServer that creates a server socket and listens for connections Needs to be started next.
        Parameters:
        thread_factory - The thread factory used to create new threads
        socket_factory - The socket factory used to create sockets
        bind_addr - The local address to bind to. If null, the address will be picked by the OS
        srv_port - The local port to bind to. If 0, the port will be picked by the OS.
        end_port - If srv_port is taken, the next port is tried, until end_port has been reached, in which case an exception will be thrown. If srv_port == end_port, only 1 port will be tried.
        external_addr - The external address in case of NAT. Ignored if null.
        external_port - The external port on the NA. If 0, srv_port is used.
        Throws:
        java.lang.Exception - Thrown if the creation failed
    • Method Detail

      • getBufferedInputStreamSize

        public int getBufferedInputStreamSize()
      • setBufferedInputStreamSize

        public TcpServer setBufferedInputStreamSize​(int s)
      • getBufferedOutputStreamSize

        public int getBufferedOutputStreamSize()
      • setBufferedOutputStreamSize

        public TcpServer setBufferedOutputStreamSize​(int s)
      • getLogAcceptError

        public boolean getLogAcceptError()
      • setLogAcceptError

        public TcpServer setLogAcceptError​(boolean l)
      • start

        public void start()
                   throws java.lang.Exception
        Description copied from class: BaseServer
        Starts accepting connections. Typically, socket handler or selectors thread are started here.
        Overrides:
        start in class BaseServer
        Throws:
        java.lang.Exception
      • stop

        public void stop()
        Description copied from class: BaseServer
        Stops listening for connections and handling traffic. Typically, socket handler or selector threads are stopped, and server sockets or channels are closed.
        Overrides:
        stop in class BaseServer