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

  • Not all enterprise applications are created equal. Sophisticated applications need developer support but other more basic apps do not. With the right tools, everyone is a potential app developer with ideas and a perspective to share. Trends such as low-code development and model driven development are fundamentally changing how and who creates applications. Is your organization ready? Read this report and learn: The seven personas of enterprise app delivery How application ownership is spreading to the …

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds