Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IPv4 addresses from Dns.GetHostEntry()

Tags:

c#

.net

ipv6

dns

ipv4

I've got some code here that works great on IPv4 machines, but on our build server (an IPv6) it fails. In a nutshell:

IPHostEntry ipHostEntry = Dns.GetHostEntry(string.Empty); 

The documentation for GetHostEntry says that passing in string.Empty will get you the IPv4 address of the localhost. This is what I want. The problem is that it's returning the string "::1:" on our IPv6 machine, which I believe is the IPv6 address.

Pinging the machine from any other IPv4 machine gives a good IPv4 address... and doing a "ping -4 machinename" from itself gives the correct IPv4 address.... but pinging it regularly from itself gives "::1:".

How can I get the IPv4 for this machine, from itself?

like image 700
zombat Avatar asked Jun 29 '09 17:06

zombat


People also ask

What is Dns GetHostEntry?

The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address. If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.

What is System Net Dns?

Remarks. The Dns class is a static class that retrieves information about a specific host from the Internet Domain Name System (DNS).

What is IPHostEntry?

The IPHostEntry class associates a Domain Name System (DNS) host name with an array of aliases and an array of matching IP addresses. The IPHostEntry class is used as a helper class with the Dns class.


1 Answers

Have you looked at all the addresses in the return, discard the ones of family InterNetworkV6 and retain only the IPv4 ones?

like image 175
Remus Rusanu Avatar answered Sep 24 '22 18:09

Remus Rusanu