System.Net.Dns.GetHostAddresses(“”)
-
25-09-2019 - |
문제
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:
- Am I doing this right?
- If you run this on your machine does it take 39 seconds to return your IP address? It does on mine.
One other note, I did a packet capture and the first request did NOT go on the wire, but the second did, and was answered quickly. So the question is what happened in XP SP3 that I am missing, besides a brain.
One last note. If I resolve a FQDN 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
해결책
See post #7 here Fix
다른 팁
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>