System.Net.Dns.GetHostAddresses-VB 2008 [英] System.Net.Dns.GetHostAddresses - VB 2008

查看:54
本文介绍了System.Net.Dns.GetHostAddresses-VB 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天很糟糕,今天看起来并不好.

我有一个正在处理的应用程序,当我的ISP由于DNS故障而启动时,启动可能会很慢.我的ISP昨天停了3个小时,所以我对添加的这段代码没有多想,直到我发现启动总是很慢.这段代码应该返回您的IP地址,我对链接的阅读表明应该立即生效,但至少在我的机器上不是这样.

昨天,在互联网中断之前,我将(oymoron)升级到XP SP3,并且遇到了其他问题.

我的问题/要求:
1.我做对了吗?
2.如果在计算机上运行此命令,是否需要39秒钟才能返回IP地址?

最后一点.如果我解析一个名字,一切都很好.

Yesterday s**ked, and today ain't (sic) looking better.

I have an application I have been working on and it can be slow to start when my ISP is down because of DNS. My ISP was down for 3 hours yesterday, so I didn't think much about this piece of code I had added, until I found that it is always slow to start. This code is supposed to return your IP address and my reading of the link suggests that should be immediate, but it isn't, at least on my machine.

Oh, and yesterday before the internet went down, I upgraded (oymoron) to XP SP3, and have had other problems.

So my questions / request:
1. Am I doing this right?
2. If you run this on your machine does it take 39 seconds to return your IP address?

One last note. If I resolve a name all is well.

 

Public Class Form1

    'http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx
    '
    'excerpt
    'The GetHostAddresses method queries a DNS server 
    'for the IP addresses associated with a host name.
    '
    'If hostNameOrAddress is an IP address, this address 
    'is returned without querying the DNS server.
    '
    'When an empty string is passed as the host name, 
    'this method returns the IPv4 addresses of the local host 

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click

        Dim stpw As New Stopwatch
        stpw.Reset()
        stpw.Start()
        'originally Dns.GetHostEntry, but slow also 
        Dim myIPs() As System.Net.IPAddress = System.Net.Dns.GetHostAddresses("")
        stpw.Stop()

        Debug.WriteLine("'" & stpw.Elapsed.TotalSeconds)
        If myIPs.Length > 0 Then Debug.WriteLine("'" & myIPs(0).ToString)
        'debug
        '39.8990525
        '192.168.1.2

        stpw.Reset()
        stpw.Start()
        'originally Dns.GetHostEntry, but slow also 
        myIPs = System.Net.Dns.GetHostAddresses("www.vbforums.com")
        stpw.Stop()

        Debug.WriteLine("'" & stpw.Elapsed.TotalSeconds)
        If myIPs.Length > 0 Then Debug.WriteLine("'" & myIPs(0).ToString)
        'debug
        '0.042212
        '63.236.73.220
    End Sub

End Class

推荐答案

如果您只是尝试返回本地计算机的IP地址,那么以下内容对我来说就可以正常工作,而不会造成任何重大延迟:

If you're just trying to return the IP address of the local machine, the following works fine for me without any significant delay:

        Dim HostName As String = System.Net.Dns.GetHostName()
        Dim IPEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(HostName)
        Dim IPAddr() As System.Net.IPAddress = IPEntry.AddressList
        Console.WriteLine("Host Name: " & HostName)
        For i As Integer = 0 To IPAddr.Length - 1
            Console.WriteLine("IP Address {0}: {1} ", i, IPAddr(i).ToString())
        Next i


这篇关于System.Net.Dns.GetHostAddresses-VB 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆