SocketConnection#

Added in version 2.22.

class SocketConnection(**properties: Any)#

Superclasses: IOStream, Object

Subclasses: TcpConnection, UnixConnection

GSocketConnection is a IOStream for a connected socket. They can be created either by SocketClient when connecting to a host, or by SocketListener when accepting a new client.

The type of the GSocketConnection object returned from these calls depends on the type of the underlying socket that is in use. For instance, for a TCP/IP connection it will be a TcpConnection.

Choosing what type of object to construct is done with the socket connection factory, and it is possible for third parties to register custom socket connection types for specific combination of socket family/type/protocol using factory_register_type.

To close a GSocketConnection, use close. Closing both substreams of the IOStream separately will not close the underlying Socket.

Methods#

class SocketConnection
connect(address: SocketAddress, cancellable: Cancellable | None = None) bool#

Connect connection to the specified remote address.

Added in version 2.32.

Parameters:
  • address – a SocketAddress specifying the remote address.

  • cancellable – a %GCancellable or None

connect_async(address: SocketAddress, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#

Asynchronously connect connection to the specified remote address.

This clears the Socket:blocking flag on connection's underlying socket if it is currently set.

If Socket:timeout is set, the operation will time out and return TIMED_OUT after that period. Otherwise, it will continue indefinitely until operating system timeouts (if any) are hit.

Use connect_finish() to retrieve the result.

Added in version 2.32.

Parameters:
  • address – a SocketAddress specifying the remote address.

  • cancellable – a %GCancellable or None

  • callback – a AsyncReadyCallback

  • user_data – user data for the callback

connect_finish(result: AsyncResult) bool#

Gets the result of a connect_async() call.

Added in version 2.32.

Parameters:

result – the AsyncResult

classmethod factory_lookup_type(type: SocketType, protocol_id: int) GType#

Looks up the Type to be used when creating socket connections on sockets with the specified family, type and protocol_id.

If no type is registered, the SocketConnection base type is returned.

Added in version 2.22.

Parameters:
  • type – a SocketType

  • protocol_id – a protocol id

classmethod factory_register_type(family: SocketFamily, type: SocketType, protocol: int) None#

Looks up the Type to be used when creating socket connections on sockets with the specified family, type and protocol.

If no type is registered, the SocketConnection base type is returned.

Added in version 2.22.

Parameters:
get_local_address() SocketAddress#

Try to get the local address of a socket connection.

Added in version 2.22.

get_remote_address() SocketAddress#

Try to get the remote address of a socket connection.

Since GLib 2.40, when used with connect() or connect_async(), during emission of CONNECTING, this function will return the remote address that will be used for the connection. This allows applications to print e.g. “Connecting to example.com (10.42.77.3)…”.

Added in version 2.22.

get_socket() Socket#

Gets the underlying Socket object of the connection. This can be useful if you want to do something unusual on it not supported by the SocketConnection APIs.

Added in version 2.22.

is_connected() bool#

Checks if connection is connected. This is equivalent to calling is_connected() on connection's underlying Socket.

Added in version 2.32.

Properties#

class SocketConnection
props.socket: Socket#

The type of the None singleton.

Added in version 2.22.

Fields#

class SocketConnection
parent_instance#
priv#