MSDN
Home Overview API Reference Samples

.NET Documentation

  • System
  • System.Collections
  • System.IO
  • System.Net
    • Http
    • Dns
    • Sockets
  • System.Text

Namespace: System.Net.Dns

Provides classes for performing Domain Name System (DNS) name resolution.

Summary

The System.Net.Dns class is the primary entry point for performing DNS lookups. It allows you to resolve hostnames to IP addresses and vice versa, as well as retrieve various DNS records.

Classes

Classes

Name Description
Dns Provides methods for resolving DNS names.
IPHostEntry Stores address information for a host.
IPAddress Represents an Internet Protocol (IP) address.

Methods

Public Static Methods

Signature Description
static IPAddress[] GetHostAddresses(string hostNameOrAddress) Resolves a host name or IP address to an array of IP addresses.
static IPHostEntry GetHostByAddress(string address) Resolves an IP address to an IPHostEntry object.
static IPHostEntry GetHostByName(string hostName) Resolves a host name to an IPHostEntry object.
static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback requestCallback, object state) Begins an asynchronous request to resolve a host name.
static IPHostEntry EndGetHostByName(IAsyncResult asyncResult) Completes an asynchronous request to resolve a host name.

Classes Details

Class Dns

Provides methods for resolving DNS names.

public static class Dns

Class IPHostEntry

Stores address information for a host.

public class IPHostEntry

Properties

  • string HostName: Gets or sets the primary DNS name of the computer.
  • string[] Aliases: Gets or sets an array of aliases for the host.
  • IPAddress[] AddressList: Gets or sets an array of IP addresses for the host.

Class IPAddress

Represents an Internet Protocol (IP) address.

public class IPAddress

Constructors

  • public IPAddress(byte[] address)
  • public IPAddress(ulong address)

Properties

  • bool IsIPv4: Gets a value indicating whether the IP address is an IPv4 address.
  • bool IsIPv6: Gets a value indicating whether the IP address is an IPv6 address.

Static Methods

  • public static IPAddress Parse(string ipString): Parses an IP address string.

Method Details

GetHostAddresses

Resolves a host name or IP address to an array of IP addresses.

public static IPAddress[] GetHostAddresses(string hostNameOrAddress)

Parameters

  • hostNameOrAddress: The host name or IP address to resolve.

Returns

An array of IPAddress objects that represent the resolved addresses.

Example

using System;
using System.Net;

// ...

try
{
    IPAddress[] addresses = Dns.GetHostAddresses("www.example.com");
    foreach (IPAddress address in addresses)
    {
        Console.WriteLine($"Resolved IP Address: {address}");
    }
}
catch (SocketException ex)
{
    Console.WriteLine($"Error resolving host: {ex.Message}");
}

GetHostByAddress

Resolves an IP address to an IPHostEntry object.

public static IPHostEntry GetHostByAddress(string address)

Parameters

  • address: The IP address to resolve.

Returns

An IPHostEntry object that contains information about the host.

Example

using System;
using System.Net;

// ...

try
{
    IPHostEntry hostEntry = Dns.GetHostByAddress("192.168.1.1");
    Console.WriteLine($"Host Name: {hostEntry.HostName}");
    Console.WriteLine("IP Addresses:");
    foreach (IPAddress ip in hostEntry.AddressList)
    {
        Console.WriteLine($"- {ip}");
    }
}
catch (SocketException ex)
{
    Console.WriteLine($"Error resolving IP address: {ex.Message}");
}

GetHostByName

Resolves a host name to an IPHostEntry object.

public static IPHostEntry GetHostByName(string hostName)

Parameters

  • hostName: The host name to resolve.

Returns

An IPHostEntry object that contains information about the host.

Example

using System;
using System.Net;

// ...

try
{
    IPHostEntry hostEntry = Dns.GetHostByName("www.microsoft.com");
    Console.WriteLine($"Host Name: {hostEntry.HostName}");
    Console.WriteLine("IP Addresses:");
    foreach (IPAddress ip in hostEntry.AddressList)
    {
        Console.WriteLine($"- {ip}");
    }
}
catch (SocketException ex)
{
    Console.WriteLine($"Error resolving host name: {ex.Message}");
}

BeginGetHostByName

Begins an asynchronous request to resolve a host name.

public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback requestCallback, object state)

Parameters

  • hostName: The host name to resolve.
  • requestCallback: An AsyncCallback delegate that references the method to invoke when the DNS lookup is complete.
  • state: A user-defined object that contains information about this asynchronous operation.

Returns

An IAsyncResult object that represents the asynchronous operation.

EndGetHostByName

Completes an asynchronous request to resolve a host name.

public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult)

Parameters

  • asyncResult: An IAsyncResult object returned by a call to BeginGetHostByName.

Returns

An IPHostEntry object that contains information about the host.

Example

using System;
using System.Net;

// ...

public class AsyncDnsResolver
{
    public static void ResolveHostAsync(string hostName)
    {
        Console.WriteLine($"Starting async resolution for: {hostName}");
        Dns.BeginGetHostByName(hostName, new AsyncCallback(CallbackMethod), hostName);
    }

    private static void CallbackMethod(IAsyncResult asyncResult)
    {
        string hostName = (string)asyncResult.AsyncState;
        try
        {
            IPHostEntry hostEntry = Dns.EndGetHostByName(asyncResult);
            Console.WriteLine($"Async resolution completed for: {hostName}");
            Console.WriteLine($"Host Name: {hostEntry.HostName}");
            Console.WriteLine("IP Addresses:");
            foreach (IPAddress ip in hostEntry.AddressList)
            {
                Console.WriteLine($"- {ip}");
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine($"Async error for {hostName}: {ex.Message}");
        }
    }
}
© 2023 Microsoft Corporation. All rights reserved.