System.Net名称空间包含了DNS类,它可以提供了编写DNS所需要的函数。下面讲解DNS类的一些方法以及在C#中如何利用该类编写程序。
一、 同步方法
在DNS类中定义了四个同步方法:GetHostName()、GetHostByName()、GetHostByAddress()、Resolve()。下面分别讲解。
1、public static string GetHostName();
功能:获取本地计算机的主机名。
返回值:字符串,包含本地计算机的 DNS 主机名。
2、public static IPHostEntry GetHostByName(string hostName)
功能:获取指定 DNS 主机名的 DNS 信息。
参数:包含主机的 DNS 名的字符串
using System;
using System.Net;
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());
}
}
}
注:IPHostEntry(为 Internet 主机地址信息提供容器类)属性介绍:
AddressList:获取或设置与主机关联的 IP 地址列表。
Aliases:获取或设置与主机关联的别名列表。
HostName:获取或设置主机的 DNS 名称。
3、GetHostByAddress()
功能:获取 IP 地址的 DNS 主机信息。
public static IPHostEntry GetHostByAddress(IPAddress);
public static IPHostEntry GetHostByAddress(string);
using System;
using System.Net;
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());
}
}
}
注:IPAddress,提供网际协议 (IP) 地址。
4、public static IPHostEntry Resolve(string hostName)
功能:将 DNS 主机名或 IP 地址解析为 IPHostEntry 实例。
参数:DNS 样式的主机名或 IP 地址。
备注:Resolve 方法在 DNS 服务器中查询与主机名或 IP 地址相关联的 IP 地址。当 hostName 是 DNS 样式的主机名并且与多个 IP 地址关联时,仅返回解析为该主机名的第一个 IP 地址。
using System;
using System.Net;
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());
}
}
}
二、 异步方法
在DNS中定义了四个异步方法: BeginGetHostByName()、BeginResolve()、EndGetHostByName()、EndResolve()
使用范例:
using System;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
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());
}
}