SocketConnection#
Added in version 2.22.
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
connectionto the specified remote address.Added in version 2.32.
- Parameters:
 address – a
SocketAddressspecifying the remote address.cancellable – a %GCancellable or
None
- async connect_async(self, address: SocketAddress) bool#
 This is the awaitable version of
connect_async().Added in version 2.32.
- Parameters:
 address – a
SocketAddressspecifying the remote address.
- connect_async(address: SocketAddress, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
 Asynchronously connect
connectionto the specified remote address.This clears the
Socket:blocking flag onconnection's underlying socket if it is currently set.If
Socket:timeout is set, the operation will time out and returnTIMED_OUTafter 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
SocketAddressspecifying the remote address.cancellable – a %GCancellable or
Nonecallback – a
AsyncReadyCallbackuser_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
Typeto be used when creating socket connections on sockets with the specifiedfamily,typeandprotocol_id.If no type is registered, the
SocketConnectionbase type is returned.Added in version 2.22.
- Parameters:
 type – a
SocketTypeprotocol_id – a protocol id
- classmethod factory_register_type(family: SocketFamily, type: SocketType, protocol: int) None#
 Looks up the
Typeto be used when creating socket connections on sockets with the specifiedfamily,typeandprotocol.If no type is registered, the
SocketConnectionbase type is returned.Added in version 2.22.
- Parameters:
 family – a
SocketFamilytype – a
SocketTypeprotocol – a protocol id
- 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()orconnect_async(), during emission ofCONNECTING, 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
Socketobject of the connection. This can be useful if you want to do something unusual on it not supported by theSocketConnectionAPIs.Added in version 2.22.
- is_connected() bool#
 Checks if
connectionis connected. This is equivalent to callingis_connected()onconnection's underlyingSocket.Added in version 2.32.