Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether an IP is from the same LAN programatically in .NET C#

Tags:

c#

.net

ip

I know that if an IP falls outside the Subnet Mask + Local IP rules, it will be only reachable through an Gateway. The problem is that I don't know how to obtain the local IP address, neither the local subnet mask, programatically using .NET. Any of you can help me?

I will use this information to squeeze the maximum performance from my batch SQL insertion queue. If the SQL server falls in the same subnet, then it will use an algorithm optimized for mininum latency, otherwise I´ll use one optimized for high latency.

like image 963
Jader Dias Avatar asked Dec 07 '22 08:12

Jader Dias


2 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
                    );
            }
        }
like image 56
Mitch Denny Avatar answered Dec 09 '22 20:12

Mitch Denny


There is an alternative way using 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 code sample is a mashup form the examples provided by Msdn, simplified to only show the information you probably need.

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

like image 34
Cohen Avatar answered Dec 09 '22 22:12

Cohen