非同步方式解析網域名稱

來源:互聯網
上載者:User

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);
  }
 }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.