Get IPv4 addresses from Dns.GetHostEntry()
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;
}
zombat
Involved in social and mobile gaming. Optimization is my jam.
Updated on February 19, 2022Comments
-
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 almost 9 yearsSystem.Net.Dns.GetHostName is a method, not a property.
-
kamranicus over 8 yearsMy 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 over 8 years@subkamran please ask that as a separate question, not as a comment
-
kamranicus over 8 yearsIt's due to Direct Access on my corporate PC. Disregard.
-
Lukazoid about 8 years@NahumLitvin But this isn't linq?
-
Nahum about 8 years@lukazoid ita not. I might have meant lambda expression
-
Storm Muller over 5 yearsWhy can't we use IPv6?
-
Remus Rusanu over 5 years@StormMuller because the OP asks "Get IPv4 addresses..."
-
Georg over 5 yearsUnfortunately 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 about 4 yearscan get 2 of ipv6 , 0 not equal ipv4, can be another set of ipv6