0

I need to know the ip address assigned to the machine in which my program is running. To achieve this, .NET Framework has the System.Net.Dns.GetHostEntry(string) method. On my machine I can call GetHostEntry passing the loopback address (127.0.0.1) and the network adapter address (10.5.8.160) and I get the same the same result. What I noticed is that on another pc if I call GetHostEntry passing the loopback address it returns just one address: 127.0.0.1. If the method is called passing the network adapter address (192.9.210.83) it returns several addresses, including the loopback (127.0.0.1 and ::1). The difference i could see is that my pc has only one ethernet connection, the other pc has two ethernet connections and a wireless one. What is the reason behind this difference?

  • "the IP address assigned to the machine" - this is not well defined. There can be any number of IP addresses (including 0) assigned to your machine. – Klaus Gütter Jun 08 '21 at 16:16
  • @KlausGütter this is exactly what I mean. Let's the the machine has 3 ips: 192.10.8.15, 10.5.8.160, 192.0.0.74. If I call `Dns.GetHostEntry("127.0.0.1")` what I get is only 127.0.0.1. If `Dns.GetHostEntry("10.5.8.160")` is called instead I get all 3 addresses listed above. What I'm tryong to undestand is why there is this difference. – Nicola Tedesco Jun 09 '21 at 06:15
  • Dns.GetHostEntry queries the DNS server for information about the host. Obviously, for 127.0.0.1, there is no DNS server to ask, so the method just returns some local information. – Klaus Gütter Jun 09 '21 at 06:50
  • It makes sense. But, in my pc calling `Dns.GetHostEntry("127.0.0.1")` and `Dns.GetHostEntry("10.5.8.159")` gives the same addresses: 127.0.0.1 and 10.5.8.159. – Nicola Tedesco Jun 09 '21 at 07:00
  • Maybe some entry in the hosts file? Useful information here: https://stackoverflow.com/questions/10910622/what-does-dns-gethostentry-methodstring-actually-do – Klaus Gütter Jun 09 '21 at 07:05

0 Answers0