`GInetAddress` represents an IPv4 or IPv6 internet address.
Use [method@Gio.Resolver.lookup_by_name] or [method@Gio.Resolver.lookup_by_name_async] to look up the `GInetAddress` for a hostname. Use [
method@Gio.Resolver.lookup_by_address] or [method@Gio.Resolver.lookup_by_address_async] to look up the hostname for a `GInetAddress`.
To actually connect to a remote host, you will need a [class@Gio.InetSocketAddress] (which includes a `GInetAddress` as well as a port number).
Example: InetAddress:
public static int main (string[] args) {
Resolver resolver = Resolver.get_default ();
// Example 1:
InetAddress address1 = new InetAddress.from_bytes ({208, 80, 152, 201}, SocketFamily.IPV4);
try {
// Output: ``wikipedia-lb.pmtpa.wikimedia.org`` (Wed Oct 24, 20012)
string hostname = resolver.lookup_by_address (address1, null);
print ("host: %s\n", hostname);
} catch (Error e) {
print ("Error: %s\n", e.message);
}
// Example 2:
InetAddress address2 = new InetAddress.from_string ("208.80.152.201");
try {
// Output: ``wikipedia-lb.pmtpa.wikimedia.org`` (Wed Oct 24, 20012)
string hostname = resolver.lookup_by_address (address2, null);
print ("host: %s\n", hostname);
} catch (Error e) {
print ("Error: %s\n", e.message);
}
// Example 3:
try {
// Resolve hostname to IP address:
string host = "www.valadoc.org";
List<InetAddress> addresses = resolver.lookup_by_name (host, null);
InetAddress address4 = addresses.nth_data (0);
// Connect:
SocketClient client = new SocketClient ();
SocketConnection conn = client.connect (new InetSocketAddress (address4, 80));
// Send HTTP GET request
string message = @"GET / HTTP/1.1\r\nHost: %s\r\n\r\n".printf (host);
conn.output_stream.write (message.data);
// Receive response
DataInputStream response = new DataInputStream (conn.input_stream);
string status_line = response.read_line (null).strip ();
print ("Received status line: %s\n", status_line);
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}
valac --pkg gio-2.0 GLib.InetAddress.vala
- public void* bytes { get; construct; }
The raw address data.
- public SocketFamily family { get; construct; }
The address family (IPv4 or IPv6).
- public uint flowinfo { get; construct; }
The flowinfo for an IPv6 address.
- public bool is_any { get; }
Whether this is the "any" address for its family.
- public bool is_link_local { get; }
Whether this is a link-local address.
- public bool is_loopback { get; }
Whether this is the loopback address for its family.
- public bool is_mc_global { get; }
Whether this is a global multicast address.
- public bool is_mc_link_local { get; }
Whether this is a link-local multicast address.
- public bool is_mc_node_local { get; }
Whether this is a node-local multicast address.
- public bool is_mc_org_local { get; }
Whether this is an organization-local multicast address.
- public bool is_mc_site_local { get; }
Whether this is a site-local multicast address.
- public bool is_multicast { get; }
Whether this is a multicast address.
- public bool is_site_local { get; }
Whether this is a site-local address.
- public uint scope_id { get; construct; }
The scope-id for an IPv6 address.
- public bool equal (InetAddress other_address)
Checks if two InetAddress instances are equal, e.
- public SocketFamily get_family ()
Gets this's family
- public uint32 get_flowinfo ()
Gets the value of [property@Gio.
- public bool get_is_any ()
Tests whether this is the "any" address
for its family.
- public bool get_is_link_local ()
Tests whether this is a link-local address
(that is, if it identifies a host on a local network that is not connected to the Internet).
- public bool get_is_loopback ()
Tests whether this is the loopback address
for its family.
- public bool get_is_mc_global ()
Tests whether this is a global multicast
address.
- public bool get_is_mc_link_local ()
Tests whether this is a link-local
multicast address.
- public bool get_is_mc_node_local ()
Tests whether this is a node-local
multicast address.
- public bool get_is_mc_org_local ()
Tests whether this is an
organization-local multicast address.
- public bool get_is_mc_site_local ()
Tests whether this is a site-local
multicast address.
- public bool get_is_multicast ()
Tests whether this is a multicast address.
- public bool get_is_site_local ()
Tests whether this is a site-local address
such as 10.0.0.1 (that is, the address identifies a host on a local network that can not be reached directly from the Internet, but which
may have outgoing Internet connectivity via a NAT or firewall).
- public size_t get_native_size ()
Gets the size of the native raw binary address for
this.
- public uint32 get_scope_id ()
Gets the value of [property@Gio.
- public virtual unowned uint8[] to_bytes ()
Gets the raw binary address data from this
.
- public virtual string to_string ()
Converts this to string form.