C# NetWork Programming里面一個例子,通過異步的方式解析域名。主要是應用Dns類的BeginResolve(),和EndResolve()方法。此例子有一個問題就是當碰到解析不出來的域名時,不會調用AsyncCallback指定的函數。我加了幾句代碼,設定2秒鐘超時時間,超時的話,顯示超時。現在的問題是,超時后并沒有結束域名的解析。超過2秒鐘解析出的域名還會顯示到listBox中。而超時后想調用Dns.EndResolve()結束解析的話會拋出異常。
有待研究。
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Threading;
namespace NetWork {
public class DnsResolver : System.Windows.Forms.Form { private System.Windows.Forms.Button btnResolve; private System.Windows.Forms.Button btnClear; private System.Windows.Forms.Button btnAbout; private System.Windows.Forms.ListBox lbResults; private System.Windows.Forms.TextBox txtHost;
private System.ComponentModel.Container components = null;
public DnsResolver() { InitializeComponent(); }
protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.btnResolve = new System.Windows.Forms.Button(); this.txtHost = new System.Windows.Forms.TextBox(); this.lbResults = new System.Windows.Forms.ListBox(); this.btnClear = new System.Windows.Forms.Button(); this.btnAbout = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnResolve // this.btnResolve.Location = new System.Drawing.Point(24, 64); this.btnResolve.Name = "btnResolve"; this.btnResolve.TabIndex = 0; this.btnResolve.Text = "&Resolve"; this.btnResolve.Click += new System.EventHandler(this.btnResolve_Click); // // txtHost // this.txtHost.Location = new System.Drawing.Point(8, 24); this.txtHost.Name = "txtHost"; this.txtHost.Size = new System.Drawing.Size(160, 21); this.txtHost.TabIndex = 1; this.txtHost.Text = ""; // // lbResults // this.lbResults.ItemHeight = 12; this.lbResults.Location = new System.Drawing.Point(176, 8); this.lbResults.Name = "lbResults"; this.lbResults.Size = new System.Drawing.Size(296, 232); this.lbResults.TabIndex = 2; // // btnClear // this.btnClear.Location = new System.Drawing.Point(24, 112); this.btnClear.Name = "btnClear"; this.btnClear.TabIndex = 3; this.btnClear.Text = "&Clear"; this.btnClear.Click += new System.EventHandler(this.btnClear_Click); // // btnAbout // this.btnAbout.Location = new System.Drawing.Point(24, 160); this.btnAbout.Name = "btnAbout"; this.btnAbout.TabIndex = 4; this.btnAbout.Text = "&About..."; this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click); // // DnsResolver // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(504, 253); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.btnAbout, this.btnClear, this.lbResults, this.txtHost, this.btnResolve}); this.Name = "DnsResolver"; this.Text = "Dns Resolver"; this.ResumeLayout(false);
} #endregion
/// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.Run(new DnsResolver()); }
void DnsResolved(IAsyncResult ar) { IPHostEntry entry=Dns.EndResolve(ar); foreach(IPAddress ip in entry.AddressList) { lbResults.Items.Add("address: "+ip.ToString()); }
foreach(string alias in entry.Aliases) { lbResults.Items.Add("alias: "+alias.ToString()); }
lbResults.Items.Add("resolve done."); }
private void btnResolve_Click(object sender, System.EventArgs e) { if( txtHost.Text=="" ) return;
lbResults.Items.Add("resolve beginning..."); IAsyncResult ar=Dns.BeginResolve(txtHost.Text,new AsyncCallback(DnsResolved),null); if (!ar.AsyncWaitHandle.WaitOne(2000,false)) { lbResults.Items.Add("two seconds elapsed, failed"); } }
private void btnClear_Click(object sender, System.EventArgs e) { lbResults.Items.Clear(); }
private void btnAbout_Click(object sender, System.EventArgs e) { string str="a tool to get the addresses & aliases of a host\n" +"by xuhx\n" +"email:xuhx20@hotmail.com/xuhx2012@sina.com"; MessageBox.Show(str,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information); } } }
|