IPHostResolver Class
Namespace: System.Net
Assembly: System.Net.Primitives
Provides methods for performing DNS lookups and translating host names to IP addresses and vice versa. This class is the primary entry point for resolving network endpoint information.
Syntax
public class IPHostResolver : object
Remarks
The IPHostResolver
class is used to resolve host names, such as "www.example.com", into IP addresses. It can also resolve IP addresses back into host names, if that information is available in DNS. This is a fundamental part of network communication, allowing applications to connect to services using easily memorable names instead of numerical IP addresses.
Instances of IPHostResolver
can be obtained from the System.Net.Dns
class.
Methods
GetHostEntry(string hostName)
Asynchronously resolves a host name to an array of IP addresses and an array of aliases.
public Task<IPHostEntry> GetHostEntryAsync(string hostName)
Parameters
Name | Type | Description |
---|---|---|
hostName |
string |
The host name to resolve. |
Returns
A Task<IPHostEntry>
that represents the asynchronous operation. The Result
property of the task contains an IPHostEntry
object that holds the IP addresses and aliases for the host.
GetHostEntry(IPAddress address)
Asynchronously resolves an IP address to a host name and an array of aliases.
public Task<IPHostEntry> GetHostEntryAsync(IPAddress address)
Parameters
Name | Type | Description |
---|---|---|
address |
IPAddress |
The IP address to resolve. |
Returns
A Task<IPHostEntry>
that represents the asynchronous operation. The Result
property of the task contains an IPHostEntry
object that holds the host name and aliases for the IP address.
GetHostAddresses(string hostName)
Asynchronously resolves a host name to an array of IP addresses.
public Task<IPAddress[]> GetHostAddressesAsync(string hostName)
Parameters
Name | Type | Description |
---|---|---|
hostName |
string |
The host name to resolve. |
Returns
A Task<IPAddress[]>
that represents the asynchronous operation. The Result
property of the task contains an array of IPAddress
objects.
Example
using System;
using System.Net;
using System.Threading.Tasks;
public class DnsLookup
{
public static async Task Main(string[] args)
{
string hostname = "www.microsoft.com";
IPHostResolver resolver = new IPHostResolver(); // In modern .NET, you'd typically use Dns.GetHostResolver() or Dns.GetHostEntryAsync() directly
try
{
Console.WriteLine($"Resolving host: {hostname}");
IPHostEntry hostEntry = await resolver.GetHostEntryAsync(hostname); // Or await Dns.GetHostEntryAsync(hostname);
Console.WriteLine($"Host name: {hostEntry.HostName}");
Console.WriteLine("IP Addresses:");
foreach (IPAddress ipAddress in hostEntry.AddressList)
{
Console.WriteLine($"- {ipAddress}");
}
if (hostEntry.Aliases.Length > 0)
{
Console.WriteLine("Aliases:");
foreach (string alias in hostEntry.Aliases)
{
Console.WriteLine($"- {alias}");
}
}
}
catch (SocketException ex)
{
Console.WriteLine($"Error resolving host: {ex.Message}");
}
}
}
System.Net.Dns
class (like Dns.GetHostEntryAsync
) are the preferred way to perform DNS lookups. The IPHostResolver
class is more of an underlying component.