GSocketConnection

GSocketConnection — A socket connection

Functions

Properties

GSocket * socket Read / Write / Construct Only

Types and Values

Object Hierarchy

    GObject
    ╰── GIOStream
        ╰── GSocketConnection
            ├── GTcpConnection
            ╰── GUnixConnection

Includes

#include <gio/gio.h>

Description

GSocketConnection is a GIOStream for a connected socket. They can be created either by GSocketClient when connecting to a host, or by GSocketListener 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 GTcpConnection.

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

To close a GSocketConnection, use g_io_stream_close(). Closing both substreams of the GIOStream separately will not close the underlying GSocket.

Functions

g_socket_connection_connect ()

gboolean
g_socket_connection_connect (GSocketConnection *connection,
                             GSocketAddress *address,
                             GCancellable *cancellable,
                             GError **error);

Connect connection to the specified remote address.

Parameters

connection

a GSocketConnection

 

address

a GSocketAddress specifying the remote address.

 

cancellable

a GCancellable or NULL.

[allow-none]

error

GError for error reporting, or NULL to ignore.

 

Returns

TRUE if the connection succeeded, FALSE on error

Since: 2.32


g_socket_connection_connect_async ()

void
g_socket_connection_connect_async (GSocketConnection *connection,
                                   GSocketAddress *address,
                                   GCancellable *cancellable,
                                   GAsyncReadyCallback callback,
                                   gpointer user_data);

Asynchronously connect connection to the specified remote address.

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

Use g_socket_connection_connect_finish() to retrieve the result.

Parameters

connection

a GSocketConnection

 

address

a GSocketAddress specifying the remote address.

 

cancellable

a GCancellable or NULL.

[allow-none]

callback

a GAsyncReadyCallback.

[scope async]

user_data

user data for the callback.

[closure]

Since: 2.32


g_socket_connection_connect_finish ()

gboolean
g_socket_connection_connect_finish (GSocketConnection *connection,
                                    GAsyncResult *result,
                                    GError **error);

Gets the result of a g_socket_connection_connect_async() call.

Parameters

connection

a GSocketConnection

 

result

the GAsyncResult

 

error

GError for error reporting, or NULL to ignore.

 

Returns

TRUE if the connection succeeded, FALSE on error

Since: 2.32


g_socket_connection_is_connected ()

gboolean
g_socket_connection_is_connected (GSocketConnection *connection);

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

Parameters

connection

a GSocketConnection

 

Returns

whether connection is connected

Since: 2.32


g_socket_connection_get_local_address ()

GSocketAddress *
g_socket_connection_get_local_address (GSocketConnection *connection,
                                       GError **error);

Try to get the local address of a socket connection.

Parameters

connection

a GSocketConnection

 

error

GError for error reporting, or NULL to ignore.

 

Returns

a GSocketAddress or NULL on error. Free the returned object with g_object_unref().

[transfer full]

Since: 2.22


g_socket_connection_get_remote_address ()

GSocketAddress *
g_socket_connection_get_remote_address
                               (GSocketConnection *connection,
                                GError **error);

Try to get the remote address of a socket connection.

Since GLib 2.40, when used with g_socket_client_connect() or g_socket_client_connect_async(), during emission of G_SOCKET_CLIENT_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)...".

Parameters

connection

a GSocketConnection

 

error

GError for error reporting, or NULL to ignore.

 

Returns

a GSocketAddress or NULL on error. Free the returned object with g_object_unref().

[transfer full]

Since: 2.22


g_socket_connection_get_socket ()

GSocket *
g_socket_connection_get_socket (GSocketConnection *connection);

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

Parameters

connection

a GSocketConnection

 

Returns

a GSocket or NULL on error.

[transfer none]

Since: 2.22


g_socket_connection_factory_create_connection ()

GSocketConnection *
g_socket_connection_factory_create_connection
                               (GSocket *socket);

Creates a GSocketConnection subclass of the right type for socket .

Parameters

socket

a GSocket

 

Returns

a GSocketConnection.

[transfer full]

Since: 2.22


g_socket_connection_factory_lookup_type ()

GType
g_socket_connection_factory_lookup_type
                               (GSocketFamily family,
                                GSocketType type,
                                gint protocol_id);

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 GSocketConnection base type is returned.

Parameters

family

a GSocketFamily

 

type

a GSocketType

 

protocol_id

a protocol id

 

Returns

a GType

Since: 2.22


g_socket_connection_factory_register_type ()

void
g_socket_connection_factory_register_type
                               (GType g_type,
                                GSocketFamily family,
                                GSocketType type,
                                gint protocol);

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 GSocketConnection base type is returned.

Parameters

g_type

a GType, inheriting from G_TYPE_SOCKET_CONNECTION

 

family

a GSocketFamily

 

type

a GSocketType

 

protocol

a protocol id

 

Since: 2.22

Types and Values

GSocketConnection

typedef struct _GSocketConnection GSocketConnection;

A socket connection GIOStream object for connection-oriented sockets.

Since: 2.22

Property Details

The “socket” property

  “socket”                   GSocket *

The underlying GSocket.

Flags: Read / Write / Construct Only

See Also

GIOStream, GSocketClient, GSocketListener