0

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
MPelletier
  • 16,256
  • 15
  • 86
  • 137
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • FWIW this code returns sub 1 second on my machine. I use opendns.com – CResults Apr 09 '10 at 16:48
  • Milliseconds for me, too, although the first one takes longer for some reason. Also, the zero index isn't guaranteed to be the IP4 address, take a look at this post: http://stackoverflow.com/questions/2546225/ip-address-lookup-in-vb-net-xp-vs-windows-7/2546536#2546536 – Chris Haas Apr 09 '10 at 16:51
  • This code is just the problem. The real code checks for IPv4 and loopback addresses. So the facts are; 1 - it runs sub-second for FQDN's, 2 - it doesn't put a packet on the wire for the code in question. So what is it that happened when I installed SP3. It is going to turn out to be something simple. – dbasnett Apr 09 '10 at 19:01
  • For what it is worth I ran the "FixIt" program from big M that is supposed to reinstall tcp / ip. No difference, except for the joy of rebooting once again. – dbasnett Apr 09 '10 at 20:31

2 Answers2

1

See post #7 here Fix

dbasnett
  • 11,334
  • 2
  • 25
  • 33
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>
dbasnett
  • 11,334
  • 2
  • 25
  • 33