Method

GioSocketClientconnect_to_host

since: 2.22

Declaration

GSocketConnection*
g_socket_client_connect_to_host (
  GSocketClient* client,
  const gchar* host_and_port,
  guint16 default_port,
  GCancellable* cancellable,
  GError** error
)

Description

This is a helper function for g_socket_client_connect().

Attempts to create a TCP connection to the named host.

host_and_port may be in any of a number of recognized formats; an IPv6 address, an IPv4 address, or a domain name (in which case a DNS lookup is performed). Quoting with [] is supported for all address types. A port override may be specified in the usual way with a colon. Ports may be given as decimal numbers or symbolic names (in which case an /etc/services lookup is performed).

If no port override is given in host_and_port then default_port will be used as the port number to connect to.

In general, host_and_port is expected to be provided by the user (allowing them to give the hostname, and a port override if necessary) and default_port is expected to be provided by the application.

In the case that an IP address is given, a single connection attempt is made. In the case that a name is given, multiple connection attempts may be made, in turn and according to the number of address records in DNS, until a connection succeeds.

Upon a successful connection, a new GSocketConnection is constructed and returned. The caller owns this new object and must drop their reference to it when finished with it.

In the event of any failure (DNS error, service not found, no hosts connectable) NULL is returned and error (if non-NULL) is set accordingly.

Available since: 2.22

Parameters

host_and_port

Type: const gchar*

The name and optionally port of the host to connect to.

The data is owned by the caller of the function.
The value is a NUL terminated UTF-8 string.
default_port

Type: guint16

The default port to connect to.

cancellable

Type: GCancellable

A GCancellable, or NULL.

The argument can be NULL.
The data is owned by the caller of the function.
error

Type: GError **

The return location for a recoverable error.

The argument can be NULL.
If the return location is not NULL, then you must initialize it to a NULL GError*.
The argument will left initialized to NULL by the method if there are no errors.
In case of error, the argument will be set to a newly allocated GError; the caller will take ownership of the data, and be responsible for freeing it.

Return value

Type: GSocketConnection

A GSocketConnection on success, NULL on error.

The caller of the method takes ownership of the data, and is responsible for freeing it.