BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Network and Internet
.NET 1.1+

Resolving Host Names and IP Addresses

Many networks, including the Internet, use the Internet Protocol (IP) for communication. All computers on such a networks have at least one IP address, consisting of a several numbers. It can be useful to resolve IP addresses to more readable host names.

Host Names and IP Addresses

All computers on IP networks have one or more IP addresses. These four-byte addresses are used to ensure that packets of data are transmitted to the correct location. They are usually written as four decimal numbers separated by full stop (period) characters. For example, a commonly used IP address for local area networks is:

192.168.0.1

A newer version of the protocol, named IPv6, has been introduced to increase the number of available addresses as demand for unique values increases. These use sixteen bytes of information and are expressed in hexadecimal with colons (:) between pairs of bytes. A sample IPv6 address is:

1234:5678:90AB:CDEF:1234:5678:90AB:CDEF

IP addresses are ideal for communication between computers and other devices. They are less useful for people to understand, particularly for organisations with many hundreds or thousands of IP addresses in use. To make it easier for users to identify computers, each device is usually given a host name. This is a name that is unique within a particular network and that is linked to a single device and its IP addresses.

When developing software that includes communication over a network, it is common to receive IP address information that you wish to convert, or resolve, to a host name. Using the .NET framework, this can be achieved with classes from the System.Net namespace.

using System.Net;

Resolving IP Addresses with .NET 1.1

The Dns class in the System.Net namespace contains a set of static methods that provide functionality relating to domain name resolution. One of these is the Resolve method, which accepts a string parameter containing either an IP address or a host name. The method returns an IPHostEntry object that holds information about the address provided, including the host name in the HostName property and an array of IP addresses in the AddressList property.

In the sample code below, the IP address 127.0.0.1 is used as this always refers to the local machine. The host name outputted will vary according to the name you have given to your computer. In the second part of the example, the host name is resolved and the first IP address from the generated list is written to the console. The IP address will depend upon your operating system settings.

IPHostEntry ipHost = Dns.GetHostEntry("127.0.0.1");
Console.WriteLine(ipHost.HostName);

IPHostEntry namedHost = Dns.Resolve("VIRTUALXP");
Console.WriteLine(namedHost.AddressList[0]);    // Outputs "192.168.0.2"

Resolving IP Addresses with .NET 2.0 and Later Versions

The Resolve method was deprecated with version 2.0 of the .NET framework. Although it still exists, a warning is generated when it is used. Instead, use the GetHostEntry method with the same argument.

IPHostEntry host = Dns.GetHostEntry("127.0.0.1");
Console.WriteLine(host.HostName);       // Outputs "VIRTUALXP"
18 May 2010