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
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 onconnection
's underlying socket if it is currently set.If
Socket
:timeout is set, the operation will time out and returnTIMED_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 specifiedfamily
,type
andprotocol_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 specifiedfamily
,type
andprotocol
.If no type is registered, the
SocketConnection
base type is returned.Added in version 2.22.
- Parameters:
family – a
SocketFamily
type – a
SocketType
protocol – 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
Socket
object of the connection. This can be useful if you want to do something unusual on it not supported by theSocketConnection
APIs.Added in version 2.22.
- is_connected() bool #
Checks if
connection
is connected. This is equivalent to callingis_connected()
onconnection
's underlyingSocket
.Added in version 2.32.