C#根据IP地址和子网掩码判断子网范围
1、根据IP地址和子网掩码判断子网范围
例如,本地IP为192.168.2.10,子网掩码为255.255.255.0,那么IP的范围:192.168.2.1~192.168.2.254。其中,当一个网段的掩码为255.255.255.0的时候,IP地址的最后一位不可以是255或0。因为这种掩码的网段,最后一位是0的IP是网段的网络地址,而最后一位是255的IP是网段的广播地址,不能分配给主机。
2、IP工具类
public static class IpAddressUtil
{
/// <summary>
/// 将字符IP转换为Uint
/// </summary>
/// <param name="strIp"></param>
/// <returns></returns>
public static uint GetIpUintFromString(this string strIp)
{
var parts = strIp.Split('.');
int nIp1 = Convert.ToInt32(parts[0]);
int nIp2 = Convert.ToInt32(parts[1]);
int nIp3 = Convert.ToInt32(parts[2]);
int nIp4 = Convert.ToInt32(parts[3]);
return (uint)((nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4);
}
/// <summary>
/// 将Uint转换为字符IP
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static string GetIPStrFromUint(this uint ip)
{
byte first = (byte)(ip >> 24);
byte second = (byte)((ip - (first << 24)) >> 16);
byte third = (byte)((ip - (second << 16) - (first << 24)) >> 8);
byte four = (byte)(ip - (second << 16) - (first << 24) - (third << 8));
return String.Format("{0}.{1}.{2}.{3}", first, second, third, four);
}
/// <summary>
/// 根据输入IP获取最小IP地址
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static uint GetLowIPStrFromUint(this uint ip)
{
byte first = (byte)(ip >> 24);
byte second = (byte)((ip - (first << 24)) >> 16);
byte third = (byte)((ip - (second << 16) - (first << 24)) >> 8);
return (uint)((first << 24) | (second << 16) | (third << 8) | 1);
}
/// <summary>
/// 根据输入IP和子网掩码获取最大IP地址
/// </summary>
/// <param name="localIp"></param>
/// <param name="subNetMask"></param>
/// <returns></returns>
public static uint GetMaxIPStrFromUint(this uint localIp, uint subNetMask)
{
byte nIp1 = (byte)(localIp >> 24);
byte nIp2 = (byte)((localIp - (nIp1 << 24)) >> 16);
byte nIp3 = (byte)((localIp - (nIp2 << 16) - (nIp1 << 24)) >> 8);
byte nIp4 = (byte)(localIp - (nIp2 << 16) - (nIp1 << 24) - (nIp3 << 8));
byte subNIp1 = (byte)(subNetMask >> 24);
byte subNIp2 = (byte)((subNetMask - (subNIp1 << 24)) >> 16);
byte subNIp3 = (byte)((subNetMask - (subNIp2 << 16) - (subNIp1 << 24)) >> 8);
byte subNIp4 = (byte)(subNetMask - (subNIp2 << 16) - (subNIp1 << 24) - (subNIp3 << 8));
int n1 = nIp1 & subNIp1;
if (n1 == 0) n1 = 255 - subNIp1;
int n2 = nIp2 & subNIp2;
if (n2 == 0) n2 = 255 - subNIp2;
int n3 = nIp3 & subNIp3;
if (n3 == 0) n3 = 255 - subNIp3;
int n4 = nIp4 & subNIp4;
if (n4 == 0)
{
// IP地址的最后一位不可以是255
if(subNIp4==0)
{
n4 = 254;
}
else
{
n4 = 255 - subNIp4;
}
}
return (uint)((n1 << 24) | (n2 << 16) | (n3 << 8) | n4);
}
}
3、IP示例
public static void Test()
{
string localIp = "192.168.2.10";
string subMask = "255.255.255.0";
uint intLocalIp = localIp.GetIpUintFromString();
uint intSubMask = subMask.GetIpUintFromString();
uint intLow = intLocalIp.GetLowIPStrFromUint();
uint intHigh = intLocalIp.GetMaxIPStrFromUint(intSubMask);
string strLow = intLow.GetIPStrFromUint();
string strHigh = intHigh.GetIPStrFromUint();
Console.WriteLine($"IP地址:{localIp}");
Console.WriteLine($"子网掩码:{localIp}");
Console.WriteLine($"IP范围:{strLow}-{strHigh}");
}
最终输出如下所示:
IP地址:192.168.2.10
子网掩码:192.168.2.10
IP范围:192.168.2.1-192.168.2.255
参考文献:(15条消息) 根据IP地址和子网掩码判断子网范围_hixixi的博客-CSDN博客_子网掩码和ip怎么判断范围