GIO Reference Manual: GNetworkAddress

## GNetworkAddress GNetworkAddress — A GSocketConnectable for resolving hostnames |

## Functions

GSocketConnectable * | g_network_address_new ()
const gchar * | g_network_address_get_hostname ()
guint16 | g_network_address_get_port ()
const gchar * | g_network_address_get_scheme ()
GSocketConnectable * | g_network_address_parse ()
GSocketConnectable * | g_network_address_parse_uri ()

## Properties

gchar * | hostname | Read / Write / Construct Only
guint | port | Read / Write / Construct Only
gchar * | scheme | Read / Write / Construct Only

## Types and Values

| GNetworkAddress

## Object Hierarchy

        GObject
        ╰── GNetworkAddress

## Implemented Interfaces

GNetworkAddress implements GSocketConnectable.

## Includes

    #include <gio/gio.h>

## Description

GNetworkAddress provides an easy way to resolve a hostname and then attempt to connect to that host, handling the possibility of multiple IP addresses and multiple address families.

See GSocketConnectable for and example of using the connectable interface.

## Functions

### g_network_address_new ()

    GSocketConnectable *
    g_network_address_new (const gchar *hostname,
                           guint16 port);

Creates a new GSocketConnectable for connecting to the given `hostname` and `port` .

#### Parameters

hostname | the hostname |
port | the port |

#### Returns

the new GNetworkAddress.

[transfer full][type GNetworkAddress]

Since 2.22

### g_network_address_get_hostname ()

    const gchar *
    g_network_address_get_hostname (GNetworkAddress *addr);

Gets `addr` 's hostname. This might be either UTF-8 or ASCII-encoded, depending on what `addr` was created with.

#### Parameters

addr | a GNetworkAddress |

#### Returns

`addr` 's hostname

Since 2.22

### g_network_address_get_port ()

    guint16
    g_network_address_get_port (GNetworkAddress *addr);

Gets `addr` 's port number

#### Parameters

addr | a GNetworkAddress |

#### Returns

`addr` 's port (which may be 0)

Since 2.22

### g_network_address_get_scheme ()

    const gchar *
    g_network_address_get_scheme (GNetworkAddress *addr);

Gets `addr` 's scheme

#### Parameters

addr | a GNetworkAddress |

#### Returns

`addr` 's scheme (`NULL` if not built from URI)

Since 2.26

### g_network_address_parse ()

    GSocketConnectable *
    g_network_address_parse (const gchar *host_and_port,
                             guint16 default_port,
                             GError **error);

Creates a new GSocketConnectable for connecting to the given `hostname` and `port` . May fail and return `NULL` in case parsing `host_and_port` fails.

`host_and_port` may be in any of a number of recognised 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.

If no port is specified 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 overide if necessary) and `default_port` is expected to be provided by the application.

(The port component of `host_and_port` can also be specified as a service name rather than as a numeric port, but this functionality is deprecated, because it depends on the contents of /etc/services, which is generally quite sparse on platforms other than Linux.)

#### Parameters

host_and_port | the hostname and optionally a port |
default_port | the default port if not in `host_and_port` |
error | a pointer to a GError, or `NULL` |

#### Returns

the new GNetworkAddress, or `NULL` on error.

[transfer full]

Since 2.22

### g_network_address_parse_uri ()

    GSocketConnectable *
    g_network_address_parse_uri (const gchar *uri,
                                 guint16 default_port,
                                 GError **error);

Creates a new GSocketConnectable for connecting to the given `uri` . May fail and return `NULL` in case parsing `uri` fails.

Using this rather than `g_network_address_new()` or `g_network_address_parse()` allows GSocketClient to determine when to use application-specific proxy protocols.

#### Parameters

uri | the hostname and optionally a port |
default_port | The default port if none is found in the URI |
error | a pointer to a GError, or `NULL` |

#### Returns

the new GNetworkAddress, or `NULL` on error.

[transfer full]

Since 2.26

## Types and Values

### GNetworkAddress

    typedef struct _GNetworkAddress GNetworkAddress;

A GSocketConnectable for resolving a hostname and connecting to that host.

## Property Details

### The `“hostname”` property

      “hostname”                 gchar *

Hostname to resolve.

Flags: Read / Write / Construct Only

Default value: NULL

### The `“port”` property

      “port”                     guint

Network port.

Flags: Read / Write / Construct Only

Allowed values: <= 65535

Default value: 0

### The `“scheme”` property

      “scheme”                   gchar *

URI Scheme.

Flags: Read / Write / Construct Only

Default value: NULL