<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; ip address</title>
	<atom:link href="http://www.dijksterhuis.org/tag/ip-address/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Converting an IP address to a Hostname, and back with C#</title>
		<link>http://www.dijksterhuis.org/converting-an-ip-address-to-a-hostname-and-back-with-c/</link>
		<comments>http://www.dijksterhuis.org/converting-an-ip-address-to-a-hostname-and-back-with-c/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 04:22:03 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[dns]]></category>
		<category><![CDATA[hostname]]></category>
		<category><![CDATA[ip address]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=273</guid>
		<description><![CDATA[
If you are using TCP/IP connections in your C# application you will need to look up DNS addresses. The following article shows how you can convert an IP address to a hostname, and a hostname to an IP address. Other common uses are to find the hostname address of the local machine, and all its [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/dns.jpg" alt="" title="DNS Lookups in C#" width="500" height="281" class="alignnone size-full wp-image-279" /></p>
<p><em>If you are using TCP/IP connections in your C# application you will need to look up DNS addresses. The following article shows how you can convert an IP address to a hostname, and a hostname to an IP address. Other common uses are to find the hostname address of the local machine, and all its IP addresses &#8212; each of which we will show in the examples below. </em></p>
<p><span id="more-273"></span></p>
<p>For this to work we need to include the <em>DNS</em> class which is part of <em>System.Net</em>.</p>
<p>As shown in the following example <em>Dns.GetHostEntry</em> is the function that does all the work. It is available from .NET 2.0 and depreciates the earlier <em>DNS.GetHostByName</em> and <em>DNS.Resolve</em> methods.</p>
<p>We use <strong>google.com</strong> in the following example to demonstrate that a single domain name can map to more than one IP address: </p>
<p><strong>Example 1: Lookup a DNS entry (name to IP)</strong></p>
<pre class="brush: c#">
            // ---------------------------------------------------------------------------------
            // Example #1 

            // Map a domain name to a set of IP addresses
            String HostName = &quot;www.google.com&quot;;
            Console.WriteLine(&quot;Looking up: {0}&quot;, HostName);

            IPHostEntry NameToIpAddress;
            NameToIpAddress = Dns.GetHostEntry(HostName);

            int AddressCount = 0;
            foreach (IPAddress Address in NameToIpAddress.AddressList)
              Console.WriteLine(&quot;IP Address {0}: {1}&quot;, ++AddressCount, Address.ToString());
</pre>
<p><strong>Example 2: Reverse DNS lookup</strong></p>
<p>Using one of the Google IP addresses I gleaned from the above example we do a reverse DNS lookup:</p>
<pre class="brush: c#">
            // ---------------------------------------------------------------------------------
            // Example #2
            //
            // Map an IP address to a hostname

            IPHostEntry IpToDomainName = Dns.GetHostEntry(&quot;209.85.173.99&quot;);
            Console.WriteLine(&quot;DomainName: {0}&quot;, IpToDomainName.HostName);
</pre>
<p><strong>Example 3: Look up the local hostname</strong></p>
<p>If you would like to know the local hostname of the machine your application is running we can find out as follows: </p>
<p><em>Note: Because the below call can throw a SocketException (if something is wrong with the Network/ Internet setup on the machine) we need to include System.Net.Sockets to be able to catch it. </em></p>
<pre class="brush: c#">
            // ---------------------------------------------------------------------------------
            // Example 3
            //
            // Lookup the machine&#039;s hostname

            String LocalHostName;

            try
            {
                LocalHostName = Dns.GetHostName();
                Console.WriteLine(&quot;Hostname: {0}&quot;, LocalHostName);
            }
            catch (SocketException e)
            {
                Console.WriteLine(&quot;SocketException caught!!!&quot;);
                Console.WriteLine(&quot;Source : &quot; + e.Source);
                Console.WriteLine(&quot;Message : &quot; + e.Message);
            }
</pre>
<p><strong>Example 4: List all IP addresses for the local host</strong></p>
<p>To obtain an array with all the IP addresses of the local machine we can call either the <em>Dns.GetHostEntry</em> or the <em>Dns.GetHostAddresses</em> method with an empty string as a parameter. In the below example we use <em>Dns.GetHostEntry</em>.</p>
<pre class="brush: c#">
            // ---------------------------------------------------------------------------------
            // Example 4
            //
            // List all the IP Addresses for the local host (&quot;&quot; refers to the local host) 

            IPHostEntry LocalEntry = Dns.GetHostEntry(&quot;&quot;);

            AddressCount = 0;
            foreach (IPAddress Address in LocalEntry.AddressList)
                Console.WriteLine(&quot;IP Address {0}: {1}&quot;, ++AddressCount, Address.ToString());
</pre>
<p><strong>Some last notes</strong></p>
<p>It should be noted that although the C# DNS class offers quite a lot of methods they all do basically the same (Hostname to IP, IP to Hostname). The .NET framework does not currently provide any functionality for looking up MX (mail exchange) records, discover nameserver or other common DNS functions. </p>
<p><small>Image credit: <a rel="nofollow" href="http://www.flickr.com/photos/memestate/">Rich Anderson</a></small></p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/converting-an-ip-address-to-a-hostname-and-back-with-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
