【问题标题】:Dns.BeginGetHostEntry won't return a resultDns.BeginGetHostEntry 不会返回结果
【发布时间】:2010-06-08 02:32:27
【问题描述】:

这是我正在使用的一段代码:

public void Start() {

    Dns.BeginGetHostEntry("www.google.com", new AsyncCallback(Stop), "Lookin up Google");
}

public void Stop(IAsyncResult ar) {
    IPHostEntry ie = Dns.EndGetHostEntry(ar);
    Console.WriteLine(ie.HostName);

    foreach(string adres in ie.Aliases) {
        Console.WriteLine(adres);
    }
}

它什么也不返回。它似乎不起作用,我没有收到任何错误。

如果我使用非异步方法:Dns.GetHostEntry("www.google.com");,那确实工作。

我不明白这里出了什么问题。

【问题讨论】:

    标签: c# dns


    【解决方案1】:

    我刚刚运行它,它运行良好

    namespace test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Start();
            }
    
            public void Start()
            {
    
                Dns.BeginGetHostEntry("www.google.com", new AsyncCallback(Stop), "Lookin up Google");
            }
    
            public void Stop(IAsyncResult ar)
            {
                IPHostEntry ie = Dns.EndGetHostEntry(ar);
                Console.WriteLine(ie.HostName);
    
                foreach (string adres in ie.Aliases)
                {
                    Console.WriteLine(adres);
                }
            }
        }
    }
    

    【讨论】:

    • 您确定要调用 Start 吗?我具体得到的输出是 www.l.google.com
    • 是的,它现在也可以工作了。前一阵子不行。另外,dirmike,这是预期的输出。
    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2016-05-28
    • 2015-08-26
    • 2023-03-25
    • 1970-01-01
    • 2019-06-18
    相关资源
    最近更新 更多