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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Stories about devastating cyberattacks are plaguing the news. Why? The DNS protocol is easy to exploit. See your network the way hackers do—as an easy target. Learn how you can effectively secure your DNS infrastructure today.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds