Gio.SocketListener

g GObject.Object GObject.Object Gio.SocketListener Gio.SocketListener GObject.Object->Gio.SocketListener

Subclasses:

Gio.SocketService

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

class

new ()

accept (cancellable)

accept_async (cancellable, callback, *user_data)

accept_finish (result)

accept_socket (cancellable)

accept_socket_async (cancellable, callback, *user_data)

accept_socket_finish (result)

add_address (address, type, protocol, source_object)

add_any_inet_port (source_object)

add_inet_port (port, source_object)

add_socket (socket, source_object)

close ()

set_backlog (listen_backlog)

Virtual Methods

Inherited:

GObject.Object (7)

do_changed ()

do_event (event, socket)

Properties

Name

Type

Flags

Short Description

listen-backlog

int

r/w/c

outstanding connections in the listen queue

Signals

Inherited:

GObject.Object (1)

Name

Short Description

event

Emitted when listener's activity on socket changes state.

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent_instance

GObject.Object

r

Class Details

class Gio.SocketListener(**kwargs)
Bases:

GObject.Object

Abstract:

No

Structure:

Gio.SocketListenerClass

A Gio.SocketListener 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 Gio.SocketListener.add_address() and Gio.SocketListener.add_inet_port(). These will be listened on until Gio.SocketListener.close() is called. Dropping your final reference to the Gio.SocketListener will not cause Gio.SocketListener.close() to be called implicitly, as some references to the Gio.SocketListener may be held internally.

If you want to implement a network server, also look at Gio.SocketService and Gio.ThreadedSocketService which are subclasses of Gio.SocketListener that make this even easier.

New in version 2.22.

classmethod new()[source]
Returns:

a new Gio.SocketListener.

Return type:

Gio.SocketListener

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

New in version 2.22.

accept(cancellable)[source]
Parameters:

cancellable (Gio.Cancellable or None) – optional Gio.Cancellable object, None to ignore.

Raises:

GLib.Error

Returns:

a Gio.SocketConnection on success, None on error.

source_object:

location where GObject.Object pointer will be stored, or None

Return type:

(Gio.SocketConnection, source_object: GObject.Object or None)

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns a Gio.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 Gio.IOErrorEnum.CANCELLED will be returned.

New in version 2.22.

accept_async(cancellable, callback, *user_data)[source]
Parameters:

This is the asynchronous version of Gio.SocketListener.accept().

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

New in version 2.22.

accept_finish(result)[source]
Parameters:

result (Gio.AsyncResult) – a Gio.AsyncResult.

Raises:

GLib.Error

Returns:

a Gio.SocketConnection on success, None on error.

source_object:

Optional GObject.Object identifying this source

Return type:

(Gio.SocketConnection, source_object: GObject.Object or None)

Finishes an async accept operation. See Gio.SocketListener.accept_async()

New in version 2.22.

accept_socket(cancellable)[source]
Parameters:

cancellable (Gio.Cancellable or None) – optional Gio.Cancellable object, None to ignore.

Raises:

GLib.Error

Returns:

a Gio.Socket on success, None on error.

source_object:

location where GObject.Object pointer will be stored, or None.

Return type:

(Gio.Socket, source_object: GObject.Object or None)

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

If you want to accept the high-level Gio.SocketConnection, not a Gio.Socket, which is often the case, then you should use Gio.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 Gio.IOErrorEnum.CANCELLED will be returned.

New in version 2.22.

accept_socket_async(cancellable, callback, *user_data)[source]
Parameters:

This is the asynchronous version of Gio.SocketListener.accept_socket().

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

New in version 2.22.

accept_socket_finish(result)[source]
Parameters:

result (Gio.AsyncResult) – a Gio.AsyncResult.

Raises:

GLib.Error

Returns:

a Gio.Socket on success, None on error.

source_object:

Optional GObject.Object identifying this source

Return type:

(Gio.Socket, source_object: GObject.Object or None)

Finishes an async accept operation. See Gio.SocketListener.accept_socket_async()

New in version 2.22.

add_address(address, type, protocol, source_object)[source]
Parameters:
Raises:

GLib.Error

Returns:

True on success, False on error.

effective_address:

location to store the address that was bound to, or None.

Return type:

(bool, effective_address: Gio.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 Gio.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 Gio.SocketListener.close() to stop listening on address; this will not be done automatically when you drop your final reference to self, as references may be held internally.

New in version 2.22.

add_any_inet_port(source_object)[source]
Parameters:

source_object (GObject.Object or None) – Optional GObject.Object identifying this source

Raises:

GLib.Error

Returns:

the port number, or 0 in case of failure.

Return type:

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.

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.

New in version 2.24.

add_inet_port(port, source_object)[source]
Parameters:
Raises:

GLib.Error

Returns:

True on success, False on error.

Return type:

bool

Helper function for Gio.SocketListener.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 Gio.SocketListener.close() to stop listening on port; this will not be done automatically when you drop your final reference to self, as references may be held internally.

New in version 2.22.

add_socket(socket, source_object)[source]
Parameters:
Raises:

GLib.Error

Returns:

True on success, False on error.

Return type:

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.

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 self 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 self, even if references to it were held elsewhere.

New in version 2.22.

close()[source]

Closes all the sockets in the listener.

New in version 2.22.

set_backlog(listen_backlog)[source]
Parameters:

listen_backlog (int) – an integer

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

See Gio.Socket.set_listen_backlog() for details

New in version 2.22.

do_changed() virtual
do_event(event, socket) virtual
Parameters:

Signal Details

Gio.SocketListener.signals.event(socket_listener, event, socket)
Signal Name:

event

Flags:

RUN_LAST

Parameters:

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.

New in version 2.46.

Property Details

Gio.SocketListener.props.listen_backlog
Name:

listen-backlog

Type:

int

Default Value:

10

Flags:

READABLE, WRITABLE, CONSTRUCT

outstanding connections in the listen queue