Skip to content

Gio.SocketConnection

class — extends IOStream

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 SocketConnection.factory_register_type.

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

Methods

connect

def connect(self, address: SocketAddress, cancellable: Cancellable | None = ...) -> bool

Connect connection to the specified remote address.

Parameters:

  • address — a SocketAddress specifying the remote address.
  • cancellable — a GCancellable or None

connect_async

def connect_async(self, address: SocketAddress, cancellable: Cancellable | None = ..., callback: Callable[[SocketConnection | None, AsyncResult], None] | 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 IOErrorEnum.TIMED_OUT after that period. Otherwise, it will continue indefinitely until operating system timeouts (if any) are hit.

Use SocketConnection.connect_finish to retrieve the result.

Parameters:

  • address — a SocketAddress specifying the remote address.
  • cancellable — a GCancellable or None
  • callback — a GAsyncReadyCallback

connect_finish

def connect_finish(self, result: AsyncResult) -> bool

Gets the result of a SocketConnection.connect_async call.

Parameters:

get_local_address

def get_local_address(self) -> SocketAddress

Try to get the local address of a socket connection.

get_remote_address

def get_remote_address(self) -> SocketAddress

Try to get the remote address of a socket connection.

Since GLib 2.40, when used with SocketClient.connect or SocketClient.connect_async, during emission of SocketClientEvent.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)...".

get_socket

def get_socket(self) -> 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.

is_connected

def is_connected(self) -> bool

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

Static functions

factory_lookup_type

@staticmethod
def factory_lookup_type(family: SocketFamily | int, type: SocketType | int, protocol_id: int) -> type | GObject.Type

Looks up the GType 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.

Parameters:

factory_register_type

@staticmethod
def factory_register_type(g_type: type | GObject.Type, family: SocketFamily | int, type: SocketType | int, protocol: int) -> None

Looks up the GType 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.

Parameters:

  • g_type — a GType, inheriting from G_TYPE_SOCKET_CONNECTION
  • family — a SocketFamily
  • type — a SocketType
  • protocol — a protocol id

Properties

socket

socket: Socket  # read/write

The underlying Socket.