[ Version ( since = "2.22" ) ]
public List<SrvTarget> lookup_service (string service, string protocol, string domain, Cancellable? cancellable = null) throws Error

Synchronously performs a DNS SRV lookup for the given service and protocol in the given domain and returns an array of SrvTarget.

domain may be an ASCII-only or UTF-8 hostname. Note also that the service and protocol arguments do not include the leading underscore that appears in the actual DNS entry.

On success, lookup_service will return a non-empty List of SrvTarget, sorted in order of preference. (That is, you should attempt to connect to the first target first, then the second if the first fails, etc.)

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 the service, it is usually easier to create a NetworkService and use its SocketConnectable interface.

Example: Resolver, service-lookup, sync (1):

public static int main () {
try {
Resolver resolver = Resolver.get_default ();
List<SrvTarget> targets = resolver.lookup_service ("pop3", "tcp", "gmail.com", null);

foreach (SrvTarget target in targets) {
print ("host: %s\n", target.get_hostname ());
print (" port: %hu\n", target.get_port ());
print (" priority: %hu\n", target.get_priority ());
print (" weight: %hu\n", target.get_weight ());
} catch (Error e) {
print ("Error: %s\n", e.message);

return 0;

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



a Resolver


the service type to look up (eg, "ldap")


the networking protocol to use for service (eg, "tcp")


the DNS domain to look up the service in


a Cancellable, or null


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