System.Net.Dns.GetHostAddresses("")

dbasnett picture dbasnett · Apr 9, 2010 · Viewed 7.5k times · Source

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? 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

Answer

dbasnett picture dbasnett · Apr 10, 2010

See post #7 here Fix