CSDNS



Click here for a larger image.

Environment: VS7, C#, .NET

This program is an example of using the DNS class to resolve host names to IP addresses and, where available, resolves host names from IP addresses. The methods for doing this are similar to the sockets functions gethostbyname and gethostbyaddr. Not all IP addresses resolve into host names; it depends on the DNS server, which may or may not have a reverse mapping defined. There also are asynchronous methods for doing the same functions. There even is a method, Dns.Resolve, that determines whether an IP address or host name is used and calls the appropriate method.

//-------------------------------------------------------
private void HostNameButtonClick(object sender,
                                 System.EventArgs e)
{
  m_status.Text = "Trying....";
  m_status.Update();
try
{
  IPHostEntry hostInfo = Dns.GetHostByName(m_host_name.Text);
  m_ip_address.Text    = hostInfo.AddressList[0].ToString();
  m_status.Text        = "ok";
}
catch (Exception excpt)
{
  m_status.Text = excpt.Message;
  m_ip_address.Text = "";
}
MessageBeep(1000);
}
//-------------------------------------------------------
private void IPAddressButtonClick(object sender,
                                  System.EventArgs e)
{
  IPAddress ipaddr = IPAddress.Parse(m_ip_address.Text);
  m_status.Text    = "Trying....";
  m_status.Update();
try
{
  IPHostEntry hostInfo = Dns.GetHostByAddress (ipaddr);
  m_host_name.Text     = hostInfo.HostName;
  m_status.Text        = "ok";
}
catch (Exception excpt)
{
  m_status.Text    = excpt.Message;
  m_host_name.Text = "";
}
  MessageBeep(1000);
}
//------------------------------------------------------
private void ResolveButtonClick(object sender,
                                System.EventArgs e)
{
  m_status.Text = "Trying....";
  m_status.Update();
try
{
  IPHostEntry hostInfo = Dns.Resolve(m_resolve_text.Text);
  m_ip_address.Text    = hostInfo.AddressList[0].ToString();
  m_host_name.Text     = hostInfo.HostName;
  m_status.Text        = "ok";
}
catch (Exception excpt)
{
  m_status.Text = excpt.Message;
  m_ip_address.Text = "";
}
  MessageBeep(1000);
}
//------------------------------------------------------

Downloads

Download demo project - 20 Kb


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds