• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

        用異步方式解析域名

        用異步方式解析域名

        更新時間:2022-05-04 文章作者:未知 信息來源:網絡 閱讀次數:

        C# NetWork Programming里面一個例子,通過異步的方式解析域名。主要是應用Dns類的BeginResolve(),和EndResolve()方法。此例子有一個問題就是當碰到解析不出來的域名時,不會調用AsyncCallback指定的函數。我加了幾句代碼,設定2秒鐘超時時間,超時的話,顯示超時,F在的問題是,超時后并沒有結束域名的解析。超過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);
        }
        }
        }

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統下載排行

        主站蜘蛛池模板: 亚洲首页在线观看| 在线观看的免费网站无遮挡 | 亚洲成aⅴ人片在线影院八| 一区二区三区免费看| 亚洲а∨天堂久久精品| 国产精品亚洲色婷婷99久久精品| 嫩草视频在线免费观看| 美女视频黄免费亚洲| 色视频色露露永久免费观看| 亚洲乱理伦片在线观看中字| 国产美女无遮挡免费网站| 亚洲色大成网站www| 日韩免费福利视频| 国产精品亚洲综合一区在线观看| 国产免费啪嗒啪嗒视频看看| 特a级免费高清黄色片| 亚洲精品国产成人片| 性无码免费一区二区三区在线| 久久亚洲AV成人无码电影| 一级女人18毛片免费| 亚洲日韩看片无码电影| 免费永久在线观看黄网站| 国产精品永久免费视频| 亚洲人成网站在线播放vr| 2019中文字幕免费电影在线播放| 亚洲av无码专区在线| 免费人妻无码不卡中文字幕18禁| 久久免费视频一区| 亚洲欧洲校园自拍都市| 四虎影视永久免费视频观看| 两个人看的www免费高清| 久久综合亚洲色HEZYO社区| 成全视频在线观看免费高清动漫视频下载 | 免费成人在线视频观看| 亚洲欧洲国产成人精品| 国产伦一区二区三区免费| 久热免费在线视频| 亚洲AV综合色区无码一二三区 | 无码一区二区三区AV免费| 福利片免费一区二区三区| 久热综合在线亚洲精品|