Csharp/C Sharp/Network/DNS

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

DNS Address Resolver

using System;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
public class AsyncResolve: Form {
   TextBox address;
   ListBox results;
   private AsyncCallback OnResolved;
   public AsyncResolve()
   {
      Text = "DNS Address Resolver";
      Size = new Size(400,380);
      OnResolved = new AsyncCallback(Resolved);

      Label label1 = new Label();
      label1.Parent = this;
      label1.Text = "Enter address to resolve:";
      label1.AutoSize = true;
      label1.Location = new Point(10, 10);
      address = new TextBox();
      address.Parent = this;
      address.Size = new Size(200, 2 * Font.Height);
      address.Location = new Point(10, 35);
      results = new ListBox();
      results.Parent = this;
      results.Location = new Point(10, 65);
      results.Size = new Size(350, 20 * Font.Height);
      Button checkit = new Button();
      checkit.Parent = this;
      checkit.Text = "Resolve";
      checkit.Location = new Point(235,32);
      checkit.Size = new Size(7 * Font.Height, 2 * Font.Height);
      checkit.Click += new EventHandler(ButtonResolveOnClick);
   }
   void ButtonResolveOnClick(object obj, EventArgs ea)
   {
      results.Items.Clear();
      string addr = address.Text;
      Object state = new Object();
      Dns.BeginResolve(addr, OnResolved, state);
   }
   private void Resolved(IAsyncResult ar)
   {
      string buffer;
      IPHostEntry iphe = Dns.EndResolve(ar);
      buffer = "Host name: " + iphe.HostName;
      results.Items.Add(buffer);
      foreach(string alias in iphe.Aliases)
      {
         buffer = "Alias: " + alias;
         results.Items.Add(buffer);
      }
      foreach(IPAddress addrs in iphe.AddressList)
      {
         buffer = "Address: " + addrs.ToString();
         results.Items.Add(buffer);
      }
   }
   public static void Main()
   {
      Application.Run(new AsyncResolve());
   }
}


DNS Get Host By Name

using System;
using System.Net;
public class DNSName
{
   public static void Main()
   {
    string hostName = Dns.GetHostName();
    Console.WriteLine("Local hostname: {0}", hostName);
    IPHostEntry myself = Dns.GetHostByName(hostName);
    foreach (IPAddress address in myself.AddressList)
    {
       Console.WriteLine("IP Address: {0}", address.ToString());
    }
   }
}


DNS Name Resolution

using System;
using System.Net;
public class DNSNameResolution
{
  [STAThread]
  static void Main(string[] args)
  {
    IPHostEntry MyHost = Dns.Resolve(args[0]);
    foreach (IPAddress MyIP in MyHost.AddressList)
    {
      Console.WriteLine(MyIP.Address);
    }
  }
}


DNS Reverse Lookup and Lookup

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
//DnsLookup
class Program {
    static void Output(IPHostEntry ipHostEntry) {
        Console.WriteLine("Host: {0}", ipHostEntry.HostName);
        Console.WriteLine("\nAliases:");
        foreach (string alias in ipHostEntry.Aliases) {
            Console.WriteLine(alias);
        }
        Console.WriteLine("\nAddress(es):");
        foreach (IPAddress address in ipHostEntry.AddressList) {
            Console.WriteLine("Address: {0}", address.ToString());
        }
        Console.ReadLine();
    }
    static void Lookup(string hostname) {
        IPHostEntry ipHostEntry = Dns.GetHostByName(hostname);
        Output(ipHostEntry);
    }
    static void ReverseLookup(string address) {
        IPHostEntry ipHostEntry = Dns.GetHostByAddress(address);
        Output(ipHostEntry);
    }
    static void Main(string[] args) {

        Lookup("www.microsoft.ru");
        ReverseLookup("172.18.1.1");
    }
}


Find DNS Servers

using System;
using Microsoft.Win32;
public class FindDNSServers
{
   public static void Main()
   {
      RegistryKey start = Registry.LocalMachine;
      string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
      RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
      if (DNSserverKey == null)
      {
         Console.WriteLine("Unable to open DNS servers key");
         return;
      }
  
      string serverlist = (string)DNSserverKey.GetValue("NameServer");
      Console.WriteLine("DNS Servers: {0}", serverlist);
      DNSserverKey.Close();
      start.Close();
      char[] token = new char[1];
      token[0] = " ";
      string[] servers = serverlist.Split(token);
      foreach(string server in servers)
      {
         Console.WriteLine("DNS server: {0}", server);
      }
   }
}


Find DNS Servers from Registry

using System;
using Microsoft.Win32;
class FindDNSServers {
    public static void Main() {
        RegistryKey start = Registry.LocalMachine;
        string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
        RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
        if (DNSserverKey == null) {
            Console.WriteLine("Unable to open DNS servers key");
            return;
        }
        string serverlist = (string)DNSserverKey.GetValue("NameServer");
        Console.WriteLine("DNS Servers: {0}", serverlist);
        DNSserverKey.Close();
        start.Close();
        char[] token = new char[1];
        token[0] = " ";
        string[] servers = serverlist.Split(token);
        foreach (string server in servers) {
            Console.WriteLine("DNS server: {0}", server);
        }
    }
}


Get DNS Address Info

using System;
using System.Net;
public class GetDNSAddressInfo {
   public static void Main(string[] argv) {
      if (argv.Length != 1) {
         Console.WriteLine("Usage: GetDNSAddressInfo address");
         return;
      }
      IPAddress test = IPAddress.Parse(argv[0]);
      IPHostEntry iphe = Dns.GetHostByAddress(test);
      Console.WriteLine("Information for {0}",
                     test.ToString());
      Console.WriteLine("Host name: {0}", iphe.HostName);
      foreach(string alias in iphe.Aliases)
      {
         Console.WriteLine("Alias: {0}", alias);
      }
      foreach(IPAddress address in iphe.AddressList)
      {
         Console.WriteLine("Address: {0}", address.ToString());
      }
   }
}


Get DNS Host Info

using System;
using System.Net;
public class GetDNSHostInfo
{
   public static void Main(string[] argv)
   {
      if (argv.Length != 1)
      {
       Console.WriteLine("Usage: GetDNSHostInfo hostname");
       return;
      }
      IPHostEntry results = Dns.GetHostByName(argv[0]);
      Console.WriteLine("Host name: {0}",
                       results.HostName);
      foreach(string alias in results.Aliases)
      {
         Console.WriteLine("Alias: {0}", alias);
      }
      foreach(IPAddress address in results.AddressList)
      {
         Console.WriteLine("Address: {0}",
                     address.ToString());
      }
   }
}


Get Resolve Info:DNS

using System;
using System.Net;
public class GetResolveInfo
{
   public static void Main(string[] argv)
   {
      if (argv.Length != 1)
      {
         Console.WriteLine("Usage: GetResolveInfo address");
         return;
      }
      IPHostEntry iphe = Dns.Resolve(argv[0]);
      Console.WriteLine("Information for {0}", argv[0]);
      Console.WriteLine("Host name: {0}", iphe.HostName);
      foreach(string alias in iphe.Aliases)
      {
         Console.WriteLine("Alias: {0}", alias);
      }
      foreach(IPAddress address in iphe.AddressList)
      {
         Console.WriteLine("Address: {0}",
                     address.ToString());
      }
   }
}