lookup_by_name


Description:

[ Version ( since = "2.22" ) ]
public virtual List<InetAddress> lookup_by_name (string hostname, Cancellable? cancellable = null) throws Error

Synchronously resolves hostname to determine its associated IP address(es).

hostname may be an ASCII-only or UTF-8 hostname, or the textual form of an IP address (in which case this just becomes a wrapper around InetAddress.from_string).

On success, lookup_by_name will return a non-empty List of InetAddress, sorted in order of preference and guaranteed to not contain duplicates. That is, if using the result to connect to hostname, you should attempt to connect to the first address first, then the second if the first fails, etc. If you are using the result to listen on a socket, it is appropriate to add each result using e.g. add_address.

If the DNS resolution fails, throws (if non-null) will be set to a value from ResolverError and null will be returned.

If cancellable is non-null, it can be used to cancel the operation, in which case throws (if non-null) will be set to g_io_error_cancelled.

If you are planning to connect to a socket on the resolved IP address, it may be easier to create a NetworkAddress and use its SocketConnectable interface.

Example: Resolver, lookup by name, sync:

public static int main () {
// Resolve hostname to IP address
try {
Resolver resolver = Resolver.get_default ();
List<InetAddress> addresses = resolver.lookup_by_name ("www.google.com", null);

// Example output:
// ``173.194.35.179``
// ``173.194.35.178``
// ``173.194.35.177``
// ``173.194.35.176``
// ``173.194.35.180``
// ``2a00:1450:4016:801::1013``
foreach (InetAddress address in addresses) {
print ("%s\n", address.to_string ());
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}

return 0;
}

valac --pkg gio-2.0 GLib.Resolver.lookup_by_name.vala

Parameters:

this

a Resolver

hostname

the hostname to look up

cancellable

a Cancellable, or null

Returns:

a non-empty List of InetAddress, or null on error. You must unref each of the addresses and free the list when you are done with it. ( You can use g_resolver_free_addresses to do this.)




2022 vala-language.org