I need to get the Local IP address of PC but if pc is connected to multiple network the ip is not correct.
I'm using:
public static string GetIPAddress()
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
Like:
I need get the local ip of adapter with internet or all local ip address.
I can see 2 options:
option 1
loop trough all interfaces using System.Net.NetworkInformation:
static List<IPAddress> GetIpAddress()
{
List<IPAddress> AllIps = new List<IPAddress>();
foreach (NetworkInterface netif in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties properties = netif.GetIPProperties();
foreach (IPAddressInformation unicast in properties.UnicastAddresses)
{
AllIps.Add(unicast.Address);
Console.WriteLine(unicast.Address);
}
}
return AllIps;
}
option 2
find the default gateway to the internet, then match the default gateway address to the interface addresses you found:
public static IPAddress GetDefaultGateway()
{
IPAddress result = null;
var cards = NetworkInterface.GetAllNetworkInterfaces().ToList();
if (cards.Any())
{
foreach (var card in cards)
{
var props = card.GetIPProperties();
if (props == null)
continue;
var gateways = props.GatewayAddresses;
if (!gateways.Any())
continue;
var gateway =
gateways.FirstOrDefault(g => g.Address.AddressFamily.ToString() == "InterNetwork");
if (gateway == null)
continue;
result = gateway.Address;
break;
};
}
return result;
}
now you can combine the 2 options in order to find the interface that is connected to the internet. here is my suggestion to match the default gateway to the correct adapter address. you can also determine the IP address classes
using the IpClass Enumeration that is written in the code i have added:
static void Main(string[] args)
{
// find all interfaces ip adressess
var allAdaptersIp = GetIpAddress();
// find the default gateway
var dg = GetDefaultGateway();
// match the default gateway to the host address => the interface that is connected to the internet => that print host address
Console.WriteLine("ip address that will route you to the world: " + GetInterNetworkHostIp(ref allAdaptersIp, dg, IpClass.ClassC));
Console.ReadLine();
}
enum IpClass
{
ClassA,
ClassB,
ClassC
}
static string GetInterNetworkHostIp(ref List<IPAddress> adapters, IPAddress dg, IpClass ipclassenum)
{
string networkAddress = "";
var result = "" ;
switch (ipclassenum)
{
case IpClass.ClassA:
networkAddress = dg.ToString().Substring(0, dg.ToString().Length - dg.ToString().LastIndexOf(".") );
break;
case IpClass.ClassB:
networkAddress = dg.ToString().Substring(0, dg.ToString().Length - dg.ToString().IndexOf(".",3));
break;
case IpClass.ClassC:
networkAddress = dg.ToString().Substring(0, dg.ToString().Length- dg.ToString().IndexOf(".") );
break;
default:
break;
}
foreach (IPAddress ip in adapters)
{
if (ip.ToString().Contains(networkAddress))
{
result = ip.ToString();
break;
}
}
if (result == "")
result = "no ip was found";
return result;
}
Related
I'm totally out of C# hence hanging a little here. I stole the code from https://stackoverflow.com/a/13175574 to read out all adapter settings available on the pc. So far so good.
What I need now is a way to check, which of the adapters are able to connect to an attached device with a given ip address.
I'd like to have a function like "bool CheckIfValidIP(IPAddress adapter, IPAddress IPv4Mask, IPAddress address)".
Can you help me here? I know it's pretty trivial :-/
Edit:
public static class IPAddressExtensions
{
public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
{
byte[] ipAdressBytes = address.GetAddressBytes();
byte[] subnetMaskBytes = subnetMask.GetAddressBytes();
if (ipAdressBytes.Length != subnetMaskBytes.Length)
throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
byte[] broadcastAddress = new byte[ipAdressBytes.Length];
for (int i = 0; i < broadcastAddress.Length; i++)
{
broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i]));
}
return new IPAddress(broadcastAddress);
}
public static bool IsInSameSubnet(IPAddress address2, IPAddress address, IPAddress subnetMask)
{
IPAddress network1 = address.GetNetworkAddress(subnetMask);
IPAddress network2 = address2.GetNetworkAddress(subnetMask);
return network1.Equals(network2);
}
}
This code shall do it. Is it safe to use?
A very simple example to get you in the right direction, this is created in .Net core 6:
using System.Net.NetworkInformation;
using System.Net.Sockets;
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
var adaptertList = new List<NetworkInterface>();
const string IpAddressToValidate = "192.168.1.1"; //Both of these values can be retrieved from appsetting.json file
const string IpV4MaskToValidate = "255.255.255.0";
adaptertList = GetValidAdapterList(interfaces);
List<NetworkInterface> GetValidAdapterList(NetworkInterface[] interfaces) {
var adaptertList = new List<NetworkInterface>();
foreach (var adapter in interfaces) {
var ipProps = adapter.GetIPProperties();
foreach (var ip in ipProps.UnicastAddresses) {
if ((adapter.OperationalStatus == OperationalStatus.Up)//Check for any conditions appropriate to your case
&& (ip.Address.AddressFamily == AddressFamily.InterNetwork)) {
if (CheckIsValidIP(ip.Address.ToString(), ip.IPv4Mask.ToString(), IpAddressToValidate, IpV4MaskToValidate)) {
adaptertList.Add(adapter);
continue;//If IP found exit the loop, as the adapter is already added
}
}
}
}
return adaptertList;
}
bool CheckIsValidIP(string ipAddress, string ipV4Mask, string validIpAddress, string validIpV4Mask) {
return (ipAddress == validIpAddress && ipV4Mask == validIpV4Mask);
}
This code will return a list of all the adapter in the machine that meet the Ip address and ipv4mask criteria.
I want to get the login-ed user country by his IP . The first function get ip address .
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
the second function takes ip and return country
public static string GetUserCountryByIp(string ip)
{
IpInfo ipInfo = new IpInfo();
try
{
string info = new WebClient().DownloadString("http://ipinfo.io/" + ip);
ipInfo = JsonConvert.DeserializeObject<IpInfo>(info);
RegionInfo myRI1 = new RegionInfo(ipInfo.Country);
ipInfo.Country = myRI1.EnglishName;
}
catch (Exception)
{
ipInfo.Country = null;
}
return ipInfo.Country;
}
the problem here is the second function doesn't return any data . when i tried my IP at https://ipinfo.io/IP it return bogon=true .
how can i return not bogon ip.
If you are wanting to get the client's IP location, you need to use the client's IP address and not the hosts. To do this in ASP.Net Core you can do:
var clientIpAddress = request.HttpContext.Connection.RemoteIpAddress;
From there you can use clientIpAddress as the IP passed to your GetUserCountryByIp function.
I'm trying to get local ip address using this code in my website:
string ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
The scenario is I try to call the website from 2 different ip address, the first call has been successfully but the second one is still displaying the first ip address.
But if I open the web using CTRL+SHIFT+R then both ip address were correct.
Somehow, the string ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; is not refreshing and carried out the cache from previous call.
How do I get around this?
Below are several codes to get local ip address but it didn't work, it can only get the server ip (IIS IP) not the local ip
//code no 1
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
//code no : 2
public static string[] GetAllLocalIPv4(NetworkInterfaceType _type)
{
List<string> ipAddrList = new List<string>();
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
{
if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
{
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
ipAddrList.Add(ip.Address.ToString());
}
}
}
}
return ipAddrList.ToArray();
}
//code no 3
public string GetLocalIpAddress2()
{
UnicastIPAddressInformation mostSuitableIp = null;
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var network in networkInterfaces)
{
if (network.OperationalStatus != OperationalStatus.Up)
continue;
var properties = network.GetIPProperties();
if (properties.GatewayAddresses.Count == 0)
continue;
foreach (var address in properties.UnicastAddresses)
{
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue;
if (IPAddress.IsLoopback(address.Address))
continue;
if (!address.IsDnsEligible)
{
if (mostSuitableIp == null)
mostSuitableIp = address;
continue;
}
// The best IP is the IP got from DHCP server
if (address.PrefixOrigin != PrefixOrigin.Dhcp)
{
if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
mostSuitableIp = address;
continue;
}
return address.Address.ToString();
}
}
return mostSuitableIp != null
? mostSuitableIp.Address.ToString()
: "";
}
//code no 4
public string GetIP4Address()
{
string IP4Address = String.Empty;
foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (IPA.AddressFamily == AddressFamily.InterNetwork)
{
IP4Address = IPA.ToString();
break;
}
}
return IP4Address;
}
Thanks,
Sam
I need to find out the local Ip address, for that reason I was using the following code:
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
I find out that when a PC has more than one IP with AddressFamily.InterNetwork I get always the first one. However I can't find any property to find out the active IP.
How can I get the correct IP?
Thanks for any tip!
I have picked this up from internet a while ago.
string strHostName = System.Net.Dns.GetHostName(); ;
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
This should give you an array of all the ip addresses of your pc.
Bwall has a fitting solution posted in this thread.
foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Console.WriteLine(ni.Name);
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Console.WriteLine(ip.Address.ToString());
}
}
}
}
The important thing in this piece of code is, that it only lists IP addresses of ethernet and wireless interfaces. I doubt you'll have a serial connection active, so this won't matter most likely. Alternativly you can always edit the if statement.
//EDIT
If you only want to IP address which actually connects to the internet use Hosam Aly's solution
This is his code:
static IPAddress getInternetIPAddress()
{
try
{
IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress gateway = IPAddress.Parse(getInternetGateway());
return findMatch(addresses, gateway);
}
catch (FormatException e) { return null; }
}
static string getInternetGateway()
{
using (Process tracert = new Process())
{
ProcessStartInfo startInfo = tracert.StartInfo;
startInfo.FileName = "tracert.exe";
startInfo.Arguments = "-h 1 www.example.com
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
tracert.Start();
using (StreamReader reader = tracert.StandardOutput)
{
string line = "";
for (int i = 0; i < 5; ++i)
line = reader.ReadLine();
line = line.Trim();
return line.Substring(line.LastIndexOf(' ') + 1);
}
}
}
static IPAddress findMatch(IPAddress[] addresses, IPAddress gateway)
{
byte[] gatewayBytes = gateway.GetAddressBytes();
foreach (IPAddress ip in addresses)
{
byte[] ipBytes = ip.GetAddressBytes();
if (ipBytes[0] == gatewayBytes[0]
&& ipBytes[1] == gatewayBytes[1]
&& ipBytes[2] == gatewayBytes[2])
{
return ip;
}
}
return null;
}
What it basically does, is trace the route to www.example.com and processes the right IP from there. I tested the code on my machine and needed to change the iterations from 9 to 5 to get the right line from stream. You better recheck it or you might into a NullReferenceException because line will be null.
static string GetActiveIP()
{
string ip = "";
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
if (f.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipInterface = f.GetIPProperties();
if (ipInterface.GatewayAddresses.Count > 0)
{`enter code here`
foreach (UnicastIPAddressInformation unicastAddress in ipInterface.UnicastAddresses)
{
if ((unicastAddress.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) && (unicastAddress.IPv4Mask.ToString() != "0.0.0.0"))
{
ip = unicastAddress.Address.ToString();
break;
}
}`enter code here`
}
}
}
return ip;
}
Let's say that I want to send an udp message to every host in my subnet (and then receive an udp message from any host in my subnet):
at the moment I do:
IPAddress broadcast = IPAddress.Parse("192.168.1.255");
but of course I want this to be done dinamically in the event that the subnet is different from 192.168.1/24. I've tried with:
IPAddress broadcast = IPAddress.Broadcast;
but IPAddress.Broadcast represents "255.255.255.255" which can't be used to send messages (it throws an exception)...so:
how do I get the local network adapter broadcast address (or netmask of course)?
THIS IS THE FINAL SOLUTION I CAME UP WITH
public IPAddress getBroadcastIP()
{
IPAddress maskIP = getHostMask();
IPAddress hostIP = getHostIP();
if (maskIP==null || hostIP == null)
return null;
byte[] complementedMaskBytes = new byte[4];
byte[] broadcastIPBytes = new byte[4];
for (int i = 0; i < 4; i++)
{
complementedMaskBytes[i] = (byte) ~ (maskIP.GetAddressBytes().ElementAt(i));
broadcastIPBytes[i] = (byte) ((hostIP.GetAddressBytes().ElementAt(i))|complementedMaskBytes[i]);
}
return new IPAddress(broadcastIPBytes);
}
private IPAddress getHostMask()
{
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface Interface in Interfaces)
{
IPAddress hostIP = getHostIP();
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
{
if (UnicatIPInfo.Address.ToString() == hostIP.ToString())
{
return UnicatIPInfo.IPv4Mask;
}
}
}
return null;
}
private IPAddress getHostIP()
{
foreach (IPAddress ip in (Dns.GetHostEntry(Dns.GetHostName())).AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip;
}
return null;
}
If you get the local IP and subnet, it should be no problem to calculate.
Something like this maybe?
using System;
using System.Net.NetworkInformation;
public class test
{
public static void Main()
{
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach(NetworkInterface Interface in Interfaces)
{
if(Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
if (Interface.OperationalStatus != OperationalStatus.Up) continue;
Console.WriteLine(Interface.Description);
UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
foreach(UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
{
Console.WriteLine("\tIP Address is {0}", UnicatIPInfo.Address);
Console.WriteLine("\tSubnet Mask is {0}", UnicatIPInfo.IPv4Mask);
}
}
}
}
How to calculate the IP range when the IP address and the netmask is given? Should give you the rest of it.