45 / Networking

DNS Resolve

resolve(hostname) is a built-in function that performs DNS resolution, converting a hostname to an IP address string. This is the first step in any network connection — before you can tcp_connect to a host, you need its IP.

45-dns-resolve.nxSource →
// DNS resolution — resolve hostnames to IP addresses
// Resolución DNS — resolver nombres de host a direcciones IP

fn main() -> int {
    // resolve() returns the IP address as a string
    let ip: String = resolve("localhost")
    print("localhost -> " + ip)

    // Resolve a public domain
    let nyx_ip: String = resolve("nyxlang.com")
    print("nyxlang.com -> " + nyx_ip)

    // Use the result to connect or display
    if nyx_ip.length() > 0 {
        print("resolution successful")
    } else {
        print("resolution failed")
    }

    return 0
}
Illustrative outputstdout
localhost -> 127.0.0.1
nyxlang.com -> 192.0.2.10
resolution successful

How it works

resolve calls the system's DNS resolver (via getaddrinfo in the runtime) and returns the first IPv4 address as a dotted-decimal string. If resolution fails, it returns an empty string.

For localhost, the result typically comes from /etc/hosts rather than a DNS query. For public domains like nyxlang.com, the runtime performs a full DNS lookup through the configured nameservers.

This is a synchronous, blocking call. For high-performance servers that need non-blocking resolution, consider running the resolve in a separate thread with thread_spawn.