System.Net.Dns Class
Namespace: System.Net
Provides name resolution functionality.
The System.Net.Dns
class provides methods for translating host names to IP addresses and IP addresses to host names. It acts as a wrapper around the underlying operating system's name resolution services.
Methods
-
static System.Net.IPHostEntry EndGetHostByName(System.IAsyncResult asyncResult)
Begins an asynchronous request to resolve a host name to an IP address. (This overload is obsolete.)
-
static System.Net.IPHostEntry GetHostByName(string hostName)
Resolves a host name to an IP address.
-
static string GetHostName()
Retrieves the local host name.
-
static System.Net.IPHostEntry GetHostEntry(string hostNameOrAddress)
Resolves a host name or IP address to an
IPHostEntry
instance. -
static System.Net.IPHostEntry GetHostEntry(System.Net.IPAddress address)
Resolves an IP address to an
IPHostEntry
instance. -
static System.Threading.Tasks.Task
GetHostEntryAsync(string hostNameOrAddress) Asynchronously resolves a host name or IP address to an
IPHostEntry
instance. -
static System.Threading.Tasks.Task
GetHostEntryAsync(System.Net.IPAddress address) Asynchronously resolves an IP address to an
IPHostEntry
instance. -
static System.Net.IPAddress[] GetHostAddresses(string hostNameOrAddress)
Retrieves the IP addresses for a host.
-
static System.Threading.Tasks.Task
GetHostAddressesAsync(string hostNameOrAddress) Asynchronously retrieves the IP addresses for a host.
-
static System.IAsyncResult BeginGetHostByName(string hostName, System.AsyncCallback requestCallback, object state)
Begins an asynchronous request to resolve a host name to an IP address. (This overload is obsolete.)
-
static System.IAsyncResult BeginGetHostEntry(string hostNameOrAddress, System.AsyncCallback requestCallback, object state)
Begins an asynchronous request to resolve a host name or IP address to an
IPHostEntry
instance. -
static System.IAsyncResult BeginGetHostEntry(System.Net.IPAddress address, System.AsyncCallback requestCallback, object state)
Begins an asynchronous request to resolve an IP address to an
IPHostEntry
instance. -
static System.IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, System.AsyncCallback requestCallback, object state)
Begins an asynchronous request to retrieve the IP addresses for a host.
-
static void EndGetHostEntry(System.IAsyncResult asyncResult)
Completes an asynchronous request to resolve a host name or IP address to an
IPHostEntry
instance. -
static System.Net.IPAddress[] EndGetHostAddresses(System.IAsyncResult asyncResult)
Completes an asynchronous request to retrieve the IP addresses for a host.
Properties
-
static int DefaultPort { get; }
Gets the default port number for DNS service.
Remarks
The Dns
class provides a simplified interface to the underlying operating system's name resolution mechanisms, such as DNS, WINS, and the local hosts file. It abstracts away the complexities of these services, allowing developers to easily translate between host names and IP addresses.
When resolving a host name, the Dns
class might query multiple name resolution services in a specific order defined by the operating system. The results from these services are aggregated, and a single IPHostEntry
object is returned.
For asynchronous operations, the Dns
class supports the .NET asynchronous programming model using IAsyncResult
. Modern .NET applications should favor the async/await
pattern with the GetHostEntryAsync
and GetHostAddressesAsync
methods.
Example
using System;
using System.Net;
public class DnsExample
{
public static void Main(string[] args)
{
try
{
// Resolve a host name to an IP address
string hostName = "www.microsoft.com";
IPHostEntry ipHostEntry = Dns.GetHostEntry(hostName);
Console.WriteLine($"Host Name: {ipHostEntry.HostName}");
Console.WriteLine("IP Addresses:");
foreach (IPAddress ipAddress in ipHostEntry.AddressList)
{
Console.WriteLine($"- {ipAddress}");
}
// Get the local host name
string localHostName = Dns.GetHostName();
Console.WriteLine($"\nLocal Host Name: {localHostName}");
}
catch (SocketException ex)
{
Console.WriteLine($"SocketException caught: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}