Resolving an IP address from a Host Name and vice versa
Posted by: Suprotim Agarwal ,
on 1/18/2008,
in
Category WinForms & WinRT
Abstract: In this article, we will use the System.Net namespace to find out the IP address by passing the host name. We will also use the IP address to find the host name. We will be using the System.Net.Dns class which can be used to retrieve information about a host.
Resolving an IP address from a Host Name and vice versa
In this article, we will use the System.Net namespace to find out the IP address by passing the host name. We will also use the IP address to find the host name. We will be using the System.Net.Dns class which can be used to retrieve information about a host.
DNS stands for Domain Name System. In simple words, a domain name is a meaningful name that identifies an internet address. DNS is a system where these domain names are located.
Retrieve IP address from Host Name
The Dns class in the System.Net namespace is a static class that provides domain name resolution functionality. The Dns.GetHostEntry() contains 2 overloads for accepting either a hostname or an IP address and returns an instance of IPHostEntry. The IPHostEntry is a helper class that can associate multiple IP addresses with the host name.
Let us see how to resolve IP addresses from a host name.
Step 1: Create a windows project. In the Form1.cs, add the System.Net namespace
C#
using System.Net;
VB.NET
Imports System.Net
Step 2: Add a Textbox (txtIP), Button control (btnIP) and a Listbox (lstIP) to the form.
Step 3: In the button click event of btnIP, type the following code:
C#
private void btnIP_Click(object sender, EventArgs e)
{
lstIP.Items.Clear();
try
{
// Host Name resolution to IP
IPHostEntry host = Dns.GetHostEntry(txtIP.Text.Trim());
IPAddress[] ipaddr = host.AddressList;
// Loop through the IP Address array and add the IP address to Listbox
foreach (IPAddress addr in ipaddr)
{
lstIP.Items.Add(addr.ToString());
}
}
// Catch unknown host names
catch (System.Net.Sockets.SocketException ex)
{
MessageBox.Show(ex.Message);
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
VB.NET
Private Sub btnIP_Click(ByVal sender As Object, ByVal e As EventArgs)
lstIP.Items.Clear()
Try
' Host Name resolution to IP
Dim host As IPHostEntry = Dns.GetHostEntry(txtIP.Text.Trim())
Dim ipaddr As IPAddress() = host.AddressList
' Loop through the IP Address array and add the IP address to Listbox
For Each addr As IPAddress In ipaddr
lstIP.Items.Add(addr.ToString())
Next addr
' Catch unknown host names
Catch ex As System.Net.Sockets.SocketException
MessageBox.Show(ex.Message)
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Just enter a hostname, say www.dotnetcurry.com and click the button. You will see the I.P addresses displayed in the list box.
Retrieve Host Name from IP Address
Step 1: Add a Textbox(txtHost), Button(btnHost) and a Label(lblHost) to the form.
Step 2: In the btnHost click, use the following code
C#
private void btnHost_Click(object sender, EventArgs e)
{
IPHostEntry host = Dns.GetHostEntry(txtHost.Text.Trim());
lblHost.Text = host.HostName;
}
VB.NET
Private Sub btnHost_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim host As IPHostEntry = Dns.GetHostEntry(txtHost.Text.Trim())
lblHost.Text = host.HostName
End Sub
Type “127.0.0.1” in textbox and click the button to see the hostname.
That was quiet easy. You saw how easy it was to use the System.Net.Dns class to resolve DNS names and IP addresses. If the hostname does not resolve in case of an invalid entry, a Socket exception is thrown back to the user.
I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn