For the .NET support, look for "System.Net" in the .NET framework.
For "educational purposes" here an example in C# that does everything
"bare bones":
(Note that some lines are different from what I've posted before, that
was theory, this is practise <g>)
using System;
using System.Text;
namespace IPDemo
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// dotted-quad notated IPs...
string dotted_quad_IP1;
string dotted_quad_IP2;
string dotted_quad_netmask1;
string dotted_quad_netmask2;
// ... become unsigned ints (32 bit)
// (long here for convenience)
long real_IP1;
long real_IP2;
long real_netmask1;
long real_netmask2;
// the results of some binary math go here
long maskIP1withnetmask1;
long maskIP2withnetmask1;
long maskIP1withnetmask2;
long maskIP2withnetmask2;
// 192.168.000.001 is 192.168.0.1,
// this crude format just for a nice output
dotted_quad_IP1 = "192.168.000.001";
dotted_quad_IP2 = "192.168.017.002";
dotted_quad_netmask1 = "255.255.255.000";
dotted_quad_netmask2 = "255.255.000.000";
// see method below
real_IP1 = DottedQuadToInt(dotted_quad_IP1);
real_IP2 = DottedQuadToInt(dotted_quad_IP2);
real_netmask1 = DottedQuadToInt(dotted_quad_netmask1);
real_netmask2 = DottedQuadToInt(dotted_quad_netmask2);
Console.WriteLine("IP1: " + dotted_quad_IP1 + " is binary: " \
+ Int2Bin(real_IP1));
Console.WriteLine("IP2: " + dotted_quad_IP2 + " is binary: " \
+ Int2Bin(real_IP2));
Console.WriteLine("Netmask1: " + dotted_quad_netmask1 + \
" is binary: " + Int2Bin(real_netmask1));
Console.WriteLine("Netmask2: " + dotted_quad_netmask2 + \
" is binary: " + Int2Bin(real_netmask2));
maskIP1withnetmask1 = real_IP1 & real_netmask1;
maskIP2withnetmask1 = real_IP2 & real_netmask1;
maskIP1withnetmask2 = real_IP1 & real_netmask2;
maskIP2withnetmask2 = real_IP2 & real_netmask2;
Console.WriteLine("IP1 masked with netmask1 results in binary: " \
+ Int2Bin(maskIP1withnetmask1));
Console.WriteLine("IP2 masked with netmask1 results in binary: " \
+ Int2Bin(maskIP2withnetmask1));
if (IPsAreInSameSubnet(real_IP1, real_IP2, real_netmask1)) {
Console.WriteLine("==> same subnet");
}
else {
Console.WriteLine("==> different subnets");
}
Console.WriteLine("IP1 masked with netmask2 results in binary: " \
+ Int2Bin(maskIP1withnetmask2));
Console.WriteLine("IP2 masked with netmask2 results in binary: " \
+ Int2Bin(maskIP2withnetmask2));
if (IPsAreInSameSubnet(real_IP1, real_IP2, real_netmask2)) {
Console.WriteLine("==> same subnet");
}
else {
Console.WriteLine("==> different subnets");
}
Console.ReadLine();
}
// convert an int into a string representing the binary format
// of the int
static string Int2Bin(long number)
{
long k;
string binaryForm = "";
for (int i = 0; i < 32; i++) {
k = number % 2;
if (k == 1) {
binaryForm = "1" + binaryForm;
}
else {
binaryForm = "0" + binaryForm;
}
// uncomment next 2 lines to have "binary dotted quad"s
// if ((i + 1) % 8 == 0 && i < 31)
// binaryForm = "." + binaryForm;
number = number / 2;
}
return binaryForm;
}
// build the int representation of a string that is a dotted
// quad IP string
static long DottedQuadToInt(string DottedQuad)
{
string[] IP_blocks = DottedQuad.Split('.');
return (Convert.ToInt64(IP_blocks[0]) << 24) | \
(Convert.ToInt64(IP_blocks[1]) << 16) | \
(Convert.ToInt64(IP_blocks[2]) << 8) | \
(Convert.ToInt64(IP_blocks[3]));
}
// check if 2 IPv4 addresses are in the same subnet
static bool IPsAreInSameSubnet(long ip1, long ip2, long netmask)
{
return ((ip1 & netmask) == (ip2 & netmask));
}
}
}