Recipe 13.1 Converting an IP Address to a Hostname

Problem

You have an IP address that you need to resolve into a hostname.

Solution

Use the Dns.Resolve method to get the hostname for an IP address. In the following code, an IP address is passed resolved, and the hostname is printed to the console:

 using System; using System.Net; //... // use the Dnss class to resolve the address  IPHostEntry iphost = Dns.Resolve("127.0.0.1");      // HostName property holds the hostname string hostName = iphost.HostName; 

Discussion

The System.Net.Dns class is provided for simple DNS resolution functionality. The Resolve method returns an IPHostEntry that can be used to access the hostname via the HostName property.

See Also

See the "DNS Class" and "IPHostEntry Class" topics in the MSDN documentation.



C# Cookbook
C# 3.0 Cookbook
ISBN: 059651610X
EAN: 2147483647
Year: 2003
Pages: 315

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net