IPAddressResolver Class

Namespace: System.Net.IP
Assembly: System.Net.Primitives.dll

Provides functionality for resolving IP addresses from host names and vice versa.

Methods

Remarks

The IPAddressResolver class is a crucial component for network programming in .NET, enabling your applications to interact with network resources using human-readable host names instead of raw IP addresses. It abstracts away the complexities of DNS (Domain Name System) lookups and other name resolution services.

This class is particularly useful when you need to:

Note that the underlying resolution mechanisms may vary depending on the operating system and network configuration. For asynchronous operations or more advanced control, consider using classes like Dns from the System.Net namespace.

Examples

Resolving a Host Name

The following example demonstrates how to resolve the IP addresses for a given host name.

using System;
using System.Net;

public class Example
{
    public static void Main(string[] args)
    {
        string hostName = "www.microsoft.com";
        try
        {
            IPAddress[] addresses = IPAddressResolver.ResolveHostByName(hostName);
            Console.WriteLine($"IP addresses for {hostName}:");
            foreach (IPAddress address in addresses)
            {
                Console.WriteLine($"- {address}");
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine($"Error resolving host {hostName}: {ex.Message}");
        }
    }
    }

Getting Host Entry by IP Address

This example shows how to retrieve detailed host information using an IP address.

using System;
using System.Net;

public class Example
{
    public static void Main(string[] args)
    {
        IPAddress ipAddress = IPAddress.Parse("8.8.8.8"); // Example IP for Google DNS
        try
        {
            IPHostEntry hostInfo = IPAddressResolver.GetHostByAddress(ipAddress);
            Console.WriteLine($"Host Name for {ipAddress}: {hostInfo.HostName}");
            Console.WriteLine("Aliases:");
            foreach (string alias in hostInfo.Aliases)
            {
                Console.WriteLine($"- {alias}");
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine($"Error resolving host by address {ipAddress}: {ex.Message}");
        }
    }
    }