Class SocketListener

All Implemented Interfaces:
PointerInterface
Direct Known Subclasses:
SocketService

public class SocketListener extends Object
A #GSocketListener is an object that keeps track of a set
of server sockets and helps you accept sockets from any of the
socket, either sync or async.

Add addresses and ports to listen on using g_socket_listener_add_address()
and g_socket_listener_add_inet_port(). These will be listened on until
g_socket_listener_close() is called. Dropping your final reference to the
#GSocketListener will not cause g_socket_listener_close() to be called
implicitly, as some references to the #GSocketListener may be held
internally.

If you want to implement a network server, also look at #GSocketService
and #GThreadedSocketService which are subclasses of #GSocketListener
that make this even easier.

https://docs.gtk.org/gio/class.SocketListener.html

  • Field Details

  • Constructor Details

    • SocketListener

      public SocketListener(PointerContainer pointer)
    • SocketListener

      public SocketListener()
      Creates a new #GSocketListener with no sockets to listen for.
      New listeners can be added with e.g. g_socket_listener_add_address()
      or g_socket_listener_add_inet_port().
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • acceptAsync

      public void acceptAsync(@Nullable Cancellable cancellable, SocketListener.OnAsyncReadyCallback callback, @Nullable Pointer user_data)
      This is the asynchronous version of g_socket_listener_accept().

      When the operation is finished @callback will be
      called. You can then call g_socket_listener_accept_finish()
      to get the result of the operation.
      Parameters:
      cancellable - a #GCancellable, or %NULL
      callback - a #GAsyncReadyCallback
      user_data - user data for the callback
    • acceptSocketAsync

      public void acceptSocketAsync(@Nullable Cancellable cancellable, SocketListener.OnAsyncReadyCallback callback, @Nullable Pointer user_data)
      This is the asynchronous version of g_socket_listener_accept_socket().

      When the operation is finished @callback will be
      called. You can then call g_socket_listener_accept_socket_finish()
      to get the result of the operation.
      Parameters:
      cancellable - a #GCancellable, or %NULL
      callback - a #GAsyncReadyCallback
      user_data - user data for the callback
    • addAnyInetPort

      public int addAnyInetPort(@Nullable Object source_object) throws AllocationError
      Listens for TCP connections on any available port number for both
      IPv6 and IPv4 (if each is available).

      This is useful if you need to have a socket for incoming connections
      but don't care about the specific port number.

      @source_object will be passed out in the various calls
      to accept to identify this particular source, which is
      useful if you're listening on multiple addresses and do
      different things depending on what address is connected to.
      Parameters:
      source_object - Optional #GObject identifying this source
      Returns:
      the port number, or 0 in case of failure.
      Throws:
      AllocationError
    • addInetPort

      public boolean addInetPort(int port, @Nullable Object source_object) throws AllocationError
      Helper function for g_socket_listener_add_address() that
      creates a TCP/IP socket listening on IPv4 and IPv6 (if
      supported) on the specified port on all interfaces.

      @source_object will be passed out in the various calls
      to accept to identify this particular source, which is
      useful if you're listening on multiple addresses and do
      different things depending on what address is connected to.

      Call g_socket_listener_close() to stop listening on @port; this will not
      be done automatically when you drop your final reference to @listener, as
      references may be held internally.
      Parameters:
      port - an IP port number (non-zero)
      source_object - Optional #GObject identifying this source
      Returns:
      %TRUE on success, %FALSE on error.
      Throws:
      AllocationError
    • addSocket

      public boolean addSocket(@Nonnull Socket socket, @Nullable Object source_object) throws AllocationError
      Adds @socket to the set of sockets that we try to accept
      new clients from. The socket must be bound to a local
      address and listened to.

      @source_object will be passed out in the various calls
      to accept to identify this particular source, which is
      useful if you're listening on multiple addresses and do
      different things depending on what address is connected to.

      The @socket will not be automatically closed when the @listener is finalized
      unless the listener held the final reference to the socket. Before GLib 2.42,
      the @socket was automatically closed on finalization of the @listener, even
      if references to it were held elsewhere.
      Parameters:
      socket - a listening #GSocket
      source_object - Optional #GObject identifying this source
      Returns:
      %TRUE on success, %FALSE on error.
      Throws:
      AllocationError
    • close

      public void close()
      Closes all the sockets in the listener.
    • setBacklog

      public void setBacklog(int listen_backlog)
      Sets the listen backlog on the sockets in the listener. This must be called
      before adding any sockets, addresses or ports to the #GSocketListener (for
      example, by calling g_socket_listener_add_inet_port()) to be effective.

      See g_socket_set_listen_backlog() for details
      Parameters:
      listen_backlog - an integer
    • onEvent

      public SignalHandler onEvent(SocketListener.OnEvent signal)
      Connect to signal "event".
      See SocketListener.OnEvent.onEvent(int, ch.bailu.gtk.gio.Socket) for signal description.
      Field SIGNAL_ON_EVENT contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()