Csharp/CSharp Tutorial/Network/DNS

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

Dns Lookup

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
   class Program
   {
      static void Main(string[] args)
      {
         if (args.Length != 1)
         {
            Console.WriteLine("Usage: DnsLookup hostname/IP Adddress");
            return;
         }
         IPHostEntry ipHostEntry = Dns.GetHostEntry(args[0]);
         Console.WriteLine("Host: {0}", ipHostEntry.HostName);
         if (ipHostEntry.Aliases.Length > 0)
         {
            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());
         }
      }
   }

DNS Name and Its IPHostEntry

using System;
using System.Net;
class MainClass
{
   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());
      }
   }
}
Local hostname: nfex
IP Address: 192.168.1.101

Get Address List from HostEntry

using System;
using System.Net;
class MainClass
{
    public static void Main(string[] args) 
    {
        foreach (string comp in args) 
        {
            try
            {
                IPAddress[] addresses = Dns.GetHostEntry(comp).AddressList;
                foreach (IPAddress address in addresses)
                {
                    Console.WriteLine("{0} = {1} ({2})", 
                        comp, address, address.AddressFamily);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} = Error ({1})", comp, ex.Message);
            }
        }
    }
}

Get DNS host name

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {

        Console.WriteLine(Dns.GetHostName());
    }
}
nfex

Get Host by IP address

using System;
using System.Net;
class MainClass
{
   public static void Main(string[] argv)
   {
      IPAddress test = IPAddress.Parse("64.200.123.1");
      IPHostEntry iphe = Dns.GetHostByAddress(test);
      Console.WriteLine("Information for {0}", test.ToString());
      Console.WriteLine("Host name: {0}", iphe.HostName);
   }
}
Information for 64.200.123.1
Host name: drvlga1wct1-atm1-0-0-12.wcg.net

Get IP address from host name

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {
        IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
      IPAddress myself = ihe.AddressList[0];
        Console.WriteLine(myself);
    }
}
192.168.1.101

Get IPAddress from IPHostEntry

using System;
using System.Net;
class MainClass
{
   public static void Main(string[] argv)
   {
      IPAddress test = IPAddress.Parse("64.200.123.1");
      IPHostEntry iphe = Dns.GetHostByAddress(test);
      foreach(IPAddress address in iphe.AddressList)
      {
         Console.WriteLine("Address: {0}", address.ToString());
      }
   }
}
Address: 64.200.123.1

Get IPHostEntry by host name

using System;
using System.Net;
class MainClass
{
   public static void Main(string[] argv)
   {
      IPHostEntry results = Dns.GetHostByName("www.nfex.ru");
      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());
      }
   }
}
Host name: nfex.ru
Alias: www.nfex.ru
Address: 68.178.206.138

Resolve a Host name

using System;
using System.Net;
class MainClass
{
   public static void Main(string[] argv)
   {
      IPHostEntry iphe = Dns.Resolve("62.208.12.1");
      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());
      }
   }
}
Host name: 62.208.12.1
Address: 62.208.12.1

Use AsyncCallback event to resolve a host name

using System;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
class MainClass
{
   private static void Resolved(IAsyncResult ar)
   {
      string buffer;
      IPHostEntry iphe = Dns.EndResolve(ar);
      buffer = "Host name: " + iphe.HostName;
      Console.WriteLine(buffer);
      foreach(string alias in iphe.Aliases)
      {
         buffer = "Alias: " + alias;
         Console.WriteLine(buffer);
      }
      foreach(IPAddress addrs in iphe.AddressList)
      {
         buffer = "Address: " + addrs.ToString();
         Console.WriteLine(buffer);
      }
   }
   public static void Main()
   {
      AsyncCallback OnResolved;
      OnResolved = new AsyncCallback(Resolved);
      string addr = "www.nfex.ru";
      Object state = new Object();
      Dns.BeginResolve(addr, OnResolved, state);
   }
}