Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello community,

I don´t have a technical issue but a question which I am thinking about to make sure that my application works properly.

Until now I´m using the following code to get the IP address of a host:
VB
Dim address As System.Net.IPAddress = System.Net.Dns.GetHostEntry("computername").AddressList(0)
So far the returned IP address was always the correct one but I want to make sure that it´s really the correct one. I assume that it´s not unproblematic to simply use the first entry in the AddressList (or am I wrong?).

So my question is how the AddressList will be orderd after gathering the addresses. Is it iterating the network interface devices alphabetically and add each IP to the AddressList? Or will they be ordered by the IP octects?

Does anyone have an idea? Is it defined at all?

Thank you all in advance!

What I have tried:

I read IPHostEntry.AddressList Property (System.Net) | Microsoft Docs[^] but didn´t find the answer.
Posted
Updated 10-Feb-19 20:38pm
v3
Comments
Maciej Los 7-Feb-19 14:40pm    
I do believe it's not ordered. It gets addresses as they're retrieved.
Richard Deeming 7-Feb-19 16:41pm    
How do you define the "correct" one? All of the addresses returned should get you to the computer, unless you have stale DNS entries.
Michael____ 8-Feb-19 9:48am    
With the "correct" one I mean the IP that I get if I use the Ping command in the CMD
console. And that leads me to another question: How is Ping.exe detecting the IP of the target computer if I´m pinging a computer name which I never pinged before?

Is it something like that?:
- Ping sends an WHOIS as broadcast
- DNS server answers
- Ping now knows the IP and is pinging it
Richard Deeming 8-Feb-19 12:07pm    
The ping utility will check the "hosts" file, NetBios, and DNS to resolve the machine name.

As far as I can see, it just picks the first address returned:
How does ping work when a host name has multiple ip addresses? - Server Fault[^]
Michael____ 8-Feb-19 13:10pm    
Thank you very much! The article behind the link answered my question.

As i mentioned in the comment to the question, i do believe it's not ordered. It gets adress(es) as it(they) is(are) retrieved.

See MSDN documentation[^]:
Quote:
The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.


For eaxmple:
Dim addresses = System.Net.Dns.GetHostEntry("www.contoso.com").AddressList

returns:
Address    AddressFamily ScopeId           IsIPv6Multicast IsIPv6LinkLocal IsIPv6SiteLocal IsIPv6Teredo IsIPv4MappedToIPv6
3143620456 InterNetwork  6SocketException4 False           False           False           False        False 
953066548  InterNetwork  6SocketException4 False           False           False           False        False 
 
Share this answer
 
I really do not believe there can be a correct order for this, given the big picture.

1. Multiple IP Addresses can be assigned to a Network Interface.
2. Multiple Network Interfaces can be installed on a computer.
3. A computer can be on multiple networks.
4. Multiple DNS servers can have entries for the same computer, with different addresses.

So what you get with GetHostList is a list of IP addresses it found from whatever DNS server(s) answered.

Examples of a connected device having multiple IPs:
- Back in the 90's have 2 modems on 2 phone lines, bridged; to increase bandwidth
- A smartphone; having a 46 connection along with WiFi and BlueTooth
- Your home router; having a public IP, a private wired IP, and a private WiFi IP

Thus.... if you ran an app using the command at home, you would most likely get different answers depending on what network was being used; WiFi or hardline for example
 
Share this answer
 
v2
Comments
Maciej Los 7-Feb-19 17:08pm    
Very nice explained!
MadMyche 7-Feb-19 17:14pm    
Thank you.
I was in Networking back in the 90s and had to draw on that ancient knowledge for the answer... and I did't even think of IP4 vs IP6
Michael____ 8-Feb-19 13:18pm    
Thank you very much for the explanation. I was aware about these circumstances but I thought there was a kind of defined order how the AddressList property will be filled.
MadMyche 8-Feb-19 13:48pm    
Well; I would think it is all going to come down to what "source" answers first. Has the address already been cached locally? Is the primary DNS server responding 1st? Remember it can take up to two days for DNS to propagate.
Many thanks to all of your for your support! I found the answer here:

How does ping work when a host name has multiple ip addresses? - Server Fault[^]

In short:
The order of the items in AddressList is not random but it´s configured on the DNS server. For my understanding that means that in a company environment I can use an index on the AddressList property because the DNS server answer order should always be the same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900