0

我有一个我一直在处理的应用程序,当我的 ISP 由于 DNS 而关闭时,启动速度可能会很慢。昨天我的 ISP 宕机了 3 个小时,所以我没有多想我添加的这段代码,直到我发现它总是启动缓慢。此代码应该返回您的 IP 地址,并且我对链接的阅读表明应该是立即的,但至少在我的机器上不是。

哦,昨天在互联网出现故障之前,我将(oymoron)升级到 XP SP3,并且遇到了其他问题。

所以我的问题/要求:

  1. 我这样做对吗?
  2. 如果你在你的机器上运行它,返回你的 IP 地址需要 39 秒吗?它在我的身上。

另一个注意事项,我做了一个数据包捕获,第一个请求没有通过网络发送,但第二个请求成功了,并且很快得到了答复。所以问题是除了大脑之外,我还缺少 XP SP3 中发生了什么。

最后一点。如果我解决了 FQDN,一切都很好。

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
4

2 回答 2

1

请参阅此处的帖子 #7修复

于 2010-04-10T12:58:37.380 回答
0
 A little power shell testing:  Comments marked *



 *returns in < 1 sec.
 PS C:\Documents and Settings\MyPC> [System.Net.Dns]::GetHostAddresses("www.msn.com")


 IPAddressToString : 65.55.17.27
 Address           : 454113089
 AddressFamily     : InterNetwork
 ScopeId           :
 IsIPv6Multicast   : False
 IsIPv6LinkLocal   : False
 IsIPv6SiteLocal   : False

 IPAddressToString : 65.55.17.26
 Address           : 437335873
 AddressFamily     : InterNetwork
 ScopeId           :
 IsIPv6Multicast   : False
 IsIPv6LinkLocal   : False
 IsIPv6SiteLocal   : False


 *takes about 40 seconds
 PS C:\Documents and Settings\MyPC> [System.Net.Dns]::GetHostAddresses("")


 IPAddressToString : 192.168.1.2
 Address           : 33663168
 AddressFamily     : InterNetwork
 ScopeId           :
 IsIPv6Multicast   : False
 IsIPv6LinkLocal   : False
 IsIPv6SiteLocal   : False


 *returns in < 1 sec.
 PS C:\Documents and Settings\MyPC> [System.Net.Dns]::GetHostAddresses("192.168.1.2")


 IPAddressToString : 192.168.1.2
 Address           : 33663168
 AddressFamily     : InterNetwork
 ScopeId           :
 IsIPv6Multicast   : False
 IsIPv6LinkLocal   : False
 IsIPv6SiteLocal   : False


 *takes about 40 seconds
 PS C:\Documents and Settings\MyPC> [System.Net.Dns]::GetHostAddresses("")


 IPAddressToString : 192.168.1.2
 Address           : 33663168
 AddressFamily     : InterNetwork
 ScopeId           :
 IsIPv6Multicast   : False
 IsIPv6LinkLocal   : False
 IsIPv6SiteLocal   : False


 *i thought this should return several addresses
 PS C:\Documents and Settings\MyPC> [System.Net.Dns]::GetHostAddresses("localhost")


 IPAddressToString : 127.0.0.1
 Address           : 16777343
 AddressFamily     : InterNetwork
 ScopeId           :
 IsIPv6Multicast   : False
 IsIPv6LinkLocal   : False
 IsIPv6SiteLocal   : False



 PS C:\Documents and Settings\MyPC>
于 2010-04-09T19:18:58.740 回答