|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "dns" for Dns
|
|
|
|
System.print("=== DNS Module Demo ===\n")
|
|
|
|
System.print("--- Basic DNS Lookup ---")
|
|
var hostname = "example.com"
|
|
System.print("Looking up: %(hostname)")
|
|
var addresses = Dns.lookup(hostname)
|
|
System.print("Addresses: %(addresses)")
|
|
|
|
System.print("\n--- IPv4 Only Lookup ---")
|
|
System.print("Looking up %(hostname) (IPv4 only)")
|
|
var ipv4Addresses = Dns.lookup(hostname, 4)
|
|
System.print("IPv4 addresses: %(ipv4Addresses)")
|
|
|
|
System.print("\n--- IPv6 Only Lookup ---")
|
|
System.print("Looking up %(hostname) (IPv6 only)")
|
|
var ipv6Addresses = Dns.lookup(hostname, 6)
|
|
System.print("IPv6 addresses: %(ipv6Addresses)")
|
|
|
|
System.print("\n--- Multiple Hostname Lookups ---")
|
|
var hosts = ["google.com", "github.com", "cloudflare.com"]
|
|
for (host in hosts) {
|
|
var addrs = Dns.lookup(host, 4)
|
|
if (addrs.count > 0) {
|
|
System.print("%(host) -> %(addrs[0])")
|
|
} else {
|
|
System.print("%(host) -> (no addresses found)")
|
|
}
|
|
}
|
|
|
|
System.print("\n--- Localhost Lookup ---")
|
|
var localhost = Dns.lookup("localhost", 4)
|
|
System.print("localhost (IPv4): %(localhost)")
|
|
|
|
System.print("\n--- Practical Example: Server Connection ---")
|
|
var serverHost = "httpbin.org"
|
|
System.print("Resolving server: %(serverHost)")
|
|
var serverAddrs = Dns.lookup(serverHost, 4)
|
|
if (serverAddrs.count > 0) {
|
|
System.print("Server IP: %(serverAddrs[0])")
|
|
System.print("Ready to connect to %(serverAddrs[0]):80")
|
|
} else {
|
|
System.print("Could not resolve %(serverHost)")
|
|
}
|