Skip to content

Gio.SocketListener

class — extends GObject.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 SocketListener.add_address and SocketListener.add_inet_port. These will be listened on until SocketListener.close is called. Dropping your final reference to the GSocketListener will not cause SocketListener.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 SocketService and ThreadedSocketService which are subclasses of GSocketListener that make this even easier.

Constructors

new

@classmethod
def new(cls) -> SocketListener

Creates a new SocketListener with no sockets to listen for. New listeners can be added with e.g. SocketListener.add_address or SocketListener.add_inet_port.

Methods

accept

def accept(self, cancellable: Cancellable | None = ...) -> tuple[SocketConnection, GObject.Object]

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns a SocketConnection for the socket that was accepted.

If source_object is not None it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If cancellable is not None, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IOErrorEnum.CANCELLED will be returned.

Parameters:

  • cancellable — optional Cancellable object, None to ignore.

accept_async

def accept_async(self, cancellable: Cancellable | None = ..., callback: Callable[[SocketListener | None, AsyncResult], None] | None = ...) -> None

This is the asynchronous version of SocketListener.accept.

When the operation is finished callback will be called. You can then call SocketListener.accept_finish to get the result of the operation.

Parameters:

  • cancellable — a Cancellable, or None
  • callback — a GAsyncReadyCallback

accept_finish

def accept_finish(self, result: AsyncResult) -> tuple[SocketConnection, GObject.Object]

Finishes an async accept operation. See SocketListener.accept_async

Parameters:

accept_socket

def accept_socket(self, cancellable: Cancellable | None = ...) -> tuple[Socket, GObject.Object]

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns the Socket that was accepted.

If you want to accept the high-level SocketConnection, not a Socket, which is often the case, then you should use SocketListener.accept instead.

If source_object is not None it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If cancellable is not None, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IOErrorEnum.CANCELLED will be returned.

Parameters:

  • cancellable — optional Cancellable object, None to ignore.

accept_socket_async

def accept_socket_async(self, cancellable: Cancellable | None = ..., callback: Callable[[SocketListener | None, AsyncResult], None] | None = ...) -> None

This is the asynchronous version of SocketListener.accept_socket.

When the operation is finished callback will be called. You can then call SocketListener.accept_socket_finish to get the result of the operation.

Parameters:

  • cancellable — a Cancellable, or None
  • callback — a GAsyncReadyCallback

accept_socket_finish

def accept_socket_finish(self, result: AsyncResult) -> tuple[Socket, GObject.Object]

Finishes an async accept operation. See SocketListener.accept_socket_async

Parameters:

add_address

def add_address(self, address: SocketAddress, type: SocketType | int, protocol: SocketProtocol | int, source_object: GObject.Object | None = ...) -> tuple[bool, SocketAddress]

Creates a socket of type type and protocol protocol, binds it to address and adds it to the set of sockets we're accepting sockets from.

Note that adding an IPv6 address, depending on the platform, may or may not result in a listener that also accepts IPv4 connections. For more deterministic behavior, see SocketListener.add_inet_port.

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.

If successful and effective_address is non-None then it will be set to the address that the binding actually occurred at. This is helpful for determining the port number that was used for when requesting a binding to port 0 (ie: "any port"). This address, if requested, belongs to the caller and must be freed.

Call SocketListener.close to stop listening on address; this will not be done automatically when you drop your final reference to listener, as references may be held internally.

Parameters:

add_any_inet_port

def add_any_inet_port(self, source_object: GObject.Object | None = ...) -> int

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.

If possible, the SocketListener will listen on both IPv4 and IPv6 (listening on the same port on both). If listening on one of the socket families fails, the SocketListener will only listen on the other. If listening on both fails, an error will be returned.

If you need to distinguish whether listening on IPv4 or IPv6 or both was successful, connect to SocketListener.event.

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:

add_inet_port

def add_inet_port(self, port: int, source_object: GObject.Object | None = ...) -> bool

Helper function for SocketListener.add_address that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.

If possible, the SocketListener will listen on both IPv4 and IPv6 (listening on the same port on both). If listening on one of the socket families fails, the SocketListener will only listen on the other. If listening on both fails, an error will be returned.

If you need to distinguish whether listening on IPv4 or IPv6 or both was successful, connect to SocketListener.event.

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 SocketListener.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.Object identifying this source

add_socket

def add_socket(self, socket: Socket, source_object: GObject.Object | None = ...) -> bool

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.

For parallel calls to SocketListener methods to work, the socket must be in non-blocking mode. (See Socket.blocking.)

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:

close

def close(self) -> None

Closes all the sockets in the listener.

set_backlog

def set_backlog(self, listen_backlog: int) -> None

Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to the SocketListener (for example, by calling SocketListener.add_inet_port) to be effective.

See Socket.set_listen_backlog for details

Parameters:

  • listen_backlog — an integer

Virtual methods

do_changed

def do_changed(self) -> None

virtual method called when the set of socket listened to changes

do_event

def do_event(self, event: SocketListenerEvent | int, socket: Socket) -> None

Properties

listen_backlog

listen_backlog: int  # read/write

The number of outstanding connections in the listen queue.

Signals

event

def on_event(self, event: SocketListenerEvent, socket: Socket) -> None: ...

Emitted when listener's activity on socket changes state. Note that when listener is used to listen on both IPv4 and IPv6, a separate set of signals will be emitted for each, and the order they happen in is undefined.