How to determine if an IP address from one LAN is programmatically in .NET C #

I know that if the IP goes beyond the subnet mask + local IP address, access to it is possible only through the gateway. The problem is that I do not know how to get the local IP address, not the local subnet mask, using .NET programmatically. Can any of you help me?

I will use this information to squeeze the maximum performance out of my package placement queue. If the SQL server falls into the same subnet, then it will use an algorithm optimized for minimum latency, otherwise I will use one optimized for high latency.

+4
source share
3 answers

You can use the classes inside the System.Net.NetworkInformation namespace (introduced in .NET 2.0):

NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface iface in interfaces) { IPInterfaceProperties properties = iface.GetIPProperties(); foreach (UnicastIPAddressInformation address in properties.UnicastAddresses) { Console.WriteLine( "{0} (Mask: {1})", address.Address, address.IPv4Mask ); } } 
+10
source

There is an alternative way to use the NetworkInformation class:

 public static void ShowNetworkInterfaces() { // IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); if (nics == null || nics.Length < 1) { Console.WriteLine(" No network interfaces found."); return; } Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length); foreach (NetworkInterface adapter in nics) { IPInterfaceProperties properties = adapter.GetIPProperties(); Console.WriteLine(); Console.WriteLine(adapter.Description); Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'=')); Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType); Console.WriteLine(" Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString()); string versions =""; // Create a display string for the supported IP versions. if (adapter.Supports(NetworkInterfaceComponent.IPv4)) { versions = "IPv4"; } if (adapter.Supports(NetworkInterfaceComponent.IPv6)) { if (versions.Length > 0) { versions += " "; } versions += "IPv6"; } Console.WriteLine(" IP version .............................. : {0}", versions); UnicastIPAddressInformationCollection uniCast = properties.UnicastAddresses; if (uniCast != null) { foreach (UnicastIPAddressInformation uni in uniCast) { Console.WriteLine(" Unicast Address ......................... : {0}", uni.Address); Console.WriteLine(" Subnet Mask ......................... : {0}", uni.IPv4Mask); } } Console.WriteLine(); } } 

The sample code is a mashup form provided by Msdn, simplified to display only the information that you probably need.

EDIT: Took me too long (too many things at the same time :)) to make this post, and Mitch beat me before him :)

+4
source

This will give you the host name and IP address. I assume that you know the IP addresses in your local network so that you can determine if the IP address is outside the local network in this way:

 // Get the host name of local machine. strHostName = Dns.GetHostName(); Console.WriteLine("Local Machine Host Name: " + strHostName); // Using the host name, get the IP address list. IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList; for (int i = 0; i < addr.Length; i++) { Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString()); } 
+1
source

All Articles