I recently needed to know the IP Address of a domain.

The goto is the trusty command line:

ping conradakunga.com

This returns the following result:

PING conradakunga.com (69.163.186.112): 56 data bytes
64 bytes from 69.163.186.112: icmp_seq=0 ttl=45 time=391.369 ms
64 bytes from 69.163.186.112: icmp_seq=1 ttl=45 time=443.522 ms
64 bytes from 69.163.186.112: icmp_seq=2 ttl=45 time=381.710 ms

Unsurprisingly, you can do this directly from your code using the Dns class, which offers a GetHostAddressesAsync method for this purpose.

You can retrieve the IPAddress given a domain name as follows:

using System.Net;

// Get the IPAddresses for the domain
var result = await Dns.GetHostAddressesAsync("conradakunga.com");
Console.WriteLine($"There were {result.Length} address(es) found");
// Cycle through and print the results
foreach (var address in result)
  Console.WriteLine(address);

This will print the following:

There were 1 address(es) found
69.163.186.112

It is entirely possible for a domain to have more than one IP address:

If we run the code for google.com

There were 2 address(es) found
172.217.170.174
2a00:1450:401a:800::200e

The second is in IPv6 address.

Is it possible to go the other way?

The best you can do is get the domain host.

The Dns class has a corresponding method to go the other way - GetHostEntryAsync

If I run it for my IPAddress:

var domain = await Dns.GetHostEntryAsync("69.163.186.112");
Console.WriteLine($"The host for this domain is {domain.HostName}");

It will print the following:

The host for this domain is apache2-twiddle.pdx1-shared-a2-12.dreamhost.com

You might wonder why we did not get back conradakunga.com? I would imagine it is because the same server on an IP address can host multiple sites.

If you want to get the actual domain name, you have to do a little work and use the WHOIS database.

The following code will retrieve this information for us:

const string whoisServer = "whois.verisign-grs.com";

using (var client = new TcpClient(whoisServer, 43))
await using (var stream = client.GetStream())
await using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
{
    await writer.WriteLineAsync("conradakunga.com");
    await writer.FlushAsync();

    // Read only the first line from the response.
    string response = (await reader.ReadLineAsync())!;
    Console.WriteLine(response);
}

This will print the following:

   Domain Name: CONRADAKUNGA.COM

Why the first line? Because a whois response looks like this:

WhoisResponse

TLDR

The Dns class has methods that allow you to retrieve an IPAddress from a domain, and a host from an IPAddress.

The code is in my GitHub.

Happy hacking!