如何计算两个IP地址是否属于同一网段?

2024-12-26 03:15:01
推荐回答(1个)
回答1:

一般我们看前3位相同基本就是同一内网的不过不也是100%现在把计算方法写给你:

public bool InSameSubNet(string maskAddress, string address1, string address2)
{
long ip0 = IPAddress.Parse(maskAddress).Address;
long ip1 = IPAddress.Parse(address1).Address;
long ip2 = IPAddress.Parse(address2).Address;

return ((ip0 & ip1) == (ip0 & ip2));
}