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

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds