Class NioClient

All Implemented Interfaces:
Closeable, AutoCloseable, Client, ConnectionListener

public class NioClient extends NioBaseServer implements Client
Since:
3.6.5
  • Field Details

  • Constructor Details

    • NioClient

      public NioClient(IpAddress bind_addr, IpAddress server_addr)
      Creates an instance of an NioClient that acts as a client: no server channel is created and no acceptor is started to listen for incoming connections. Instead, a channel is created (bound to bind_addr/bind_port) and connected to server_addr/server_port. This is used to send messages to the remote server and receive messages from it. Note that there is only a single TCP connection established between the client and server.
      Parameters:
      bind_addr - The address to which the local channel should bind to. Can be null, then the OS picks the address
      server_addr - The address of the server to connect to
    • NioClient

      public NioClient(InetAddress bind_addr, int bind_port, InetAddress server_addr, int server_port)
      Creates an instance of an NioClient that acts as a client: no server channel is created and no acceptor is started to listen for incoming connections. Instead, a channel is created (bound to bind_addr/bind_port) and connected to server_addr/server_port. This is used to send messages to the remote server and receive messages from it. Note that there is only a single TCP connection established between the client and server.
      Parameters:
      bind_addr - The address to which the local channel should bind to. Can be null, then the OS picks the address
      bind_port - The local port. Can be 0, then the OS picks the port.
      server_addr - The address of the server to connect to
      server_port - The port of the server to connect to.
  • Method Details

    • remoteAddress

      public Address remoteAddress()
      Specified by:
      remoteAddress in interface Client
    • remoteAddress

      public NioClient remoteAddress(IpAddress addr)
      Sets the address of the server. Has no effect when already connected.
    • isConnected

      public boolean isConnected()
      Specified by:
      isConnected in interface Client
    • start

      public void start() throws Exception
      Description copied from class: BaseServer
      Starts accepting connections. Typically, socket handler or selectors thread are started here.
      Overrides:
      start in class BaseServer
      Throws:
      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
    • send

      public void send(Address dest, ByteBuffer data) throws Exception
      Overrides:
      send in class BaseServer
      Throws:
      Exception
    • send

      public void send(Address dest, byte[] data, int offset, int length) throws Exception
      Overrides:
      send in class BaseServer
      Throws:
      Exception
    • send

      public void send(byte[] data, int offset, int length) throws Exception
      Description copied from interface: Client
      Sends data to the remote server. The server's address must have been set before.
      Specified by:
      send in interface Client
      Throws:
      Exception
    • send

      public void send(ByteBuffer data) throws Exception
      Description copied from interface: Client
      Sends data to the remote server. The server's address must have been set before.
      Specified by:
      send in interface Client
      Throws:
      Exception
    • toString

      public String toString()
      Overrides:
      toString in class BaseServer
    • doStart

      protected void doStart() throws Exception
      Throws:
      Exception