Get IPv4 addresses from Dns.GetHostEntry()

111,476

Solution 1

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

Solution 2

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

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

Solution 4

    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

    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;
    }
Share:
111,476
zombat
Author by

zombat

Involved in social and mobile gaming. Optimization is my jam.

Updated on February 19, 2022

Comments

  • zombat
    zombat about 2 years

    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?

  • RenniePet
    RenniePet almost 9 years
    System.Net.Dns.GetHostName is a method, not a property.
  • kamranicus
    kamranicus over 8 years
    My call is only returning ipv6, no ipv4 entries. If I do Resolve-DnsName -Type A in PowerShell I get the IPv4 back (default type it returns in this case is AAAA).
  • Remus Rusanu
    Remus Rusanu over 8 years
    @subkamran please ask that as a separate question, not as a comment
  • kamranicus
    kamranicus over 8 years
    It's due to Direct Access on my corporate PC. Disregard.
  • Lukazoid
    Lukazoid about 8 years
    @NahumLitvin But this isn't linq?
  • Nahum
    Nahum about 8 years
    @lukazoid ita not. I might have meant lambda expression
  • Storm Muller
    Storm Muller over 5 years
    Why can't we use IPv6?
  • Remus Rusanu
    Remus Rusanu over 5 years
    @StormMuller because the OP asks "Get IPv4 addresses..."
  • Georg
    Georg over 5 years
    Unfortunately is the VirtualBox-address before computer ip address on my machine. I can't select the real ip address because both have the "InterNetwork" flag.
  • Admin
    Admin about 4 years
    can get 2 of ipv6 , 0 not equal ipv4, can be another set of ipv6