Tuesday, 23 August 2011

Writing a simple app to resolve IP Address from host name

In Linux and Windows we can use various command line utilities to resolve IP address from a host name. Here I am using visual studio 2010 to create a simple GUI app that will give the ip address of a user supplied hostname. It very simple to create an app like this using vs 2010 because we can make use of the existing base class libraries to accomplish the task.

First Open visual Studio->Create New Project

Language C# and choose windows Form Project.

For the GUI Part we need two text fields one for host name and one for IP Address. Also a button named  “Get IP”.In the click event of the button we write code to get ip address.After designing the UI,It will look like this

 To get the IP Address we need to use the following System.Net namespace like this
using System.Net;
 The above name space contains a class called  IPHostEntry and using the method Dns.GetHostEntry(),We can get the IP Address corresponding to a host name :-)
 IPHostEntry iHost = null;
 iHost = Dns.GetHostEntry(hostName);
 txt_ip.Text = iHost.AddressList[0].ToString();
To download the solution and executable file please click here