Get IPv4 addresses from Dns.GetHostEntry()

C#.NetDnsIpv6Ipv4

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

To find all local IPv4 addresses:

IPAddress[] ipv4Addresses = Array.FindAll(
    Dns.GetHostEntry(string.Empty).AddressList,
    a => a.AddressFamily == AddressFamily.InterNetwork);

or use Array.Find or Array.FindLast if you just want one.

Solution 3 - C#

IPHostEntry ipHostInfo = Dns.GetHostEntry(serverName);
IPAddress ipAddress = ipHostInfo.AddressList
    .FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);

Solution 4 - C#

    public Form1()
    {
        InitializeComponent();

        string myHost = System.Net.Dns.GetHostName();
        string myIP = null;

        for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
        {
            if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
            {
                myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
            }
        }
    }

Declare myIP and myHost in public Variable and use in any function of the form.

Solution 5 - C#

	public static string GetIPAddress(string hostname)
	{
		IPHostEntry host;
		host = Dns.GetHostEntry(hostname);

		foreach (IPAddress ip in host.AddressList)
		{
			if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
			{
				//System.Diagnostics.Debug.WriteLine("LocalIPadress: " + ip);
				return ip.ToString();
			}
		}
		return string.Empty;
	}

Solution 6 - C#

To find all valid address list this is the code I have used

public static IEnumerable<string> GetAddresses()
{
      var host = Dns.GetHostEntry(Dns.GetHostName());
      return (from ip in host.AddressList where ip.AddressFamily == AddressFamily.lo select ip.ToString()).ToList();
}

Solution 7 - C#

You can get all IPv4 address from the DNS using this code:

IPs[] ipv4Addresses = Array.FindAll(
           Dns.GetHostEntry(string.Empty).AddressList,
           address => address.AddressFamily == AddressFamily.InterNetwork);

Solution 8 - C#

IPv6

lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()


IPv4

lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(1).ToString()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzombatView Question on Stackoverflow
Solution 1 - C#Remus RusanuView Answer on Stackoverflow
Solution 2 - C#GaryView Answer on Stackoverflow
Solution 3 - C#Milan ŠvecView Answer on Stackoverflow
Solution 4 - C#Naveen DesoshaView Answer on Stackoverflow
Solution 5 - C#Ronald van ZoelenView Answer on Stackoverflow
Solution 6 - C#Ravi ShankarView Answer on Stackoverflow
Solution 7 - C#haqsek2 groupView Answer on Stackoverflow
Solution 8 - C#not norView Answer on Stackoverflow