0

在java中有一个方法inetaddress.isSiteLocalAddrress来确定IP地址是否与站点相同,我们在C#中是否有任何等效的方法

4

1 回答 1

0
private bool isIPLocal(IPAddress ipaddress)
{
    String[] straryIPAddress = ipaddress.ToString().Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries);
    int[] iaryIPAddress = new int[] { int.Parse(straryIPAddress[0]), int.Parse(straryIPAddress[1]), int.Parse(straryIPAddress[2]), int.Parse(straryIPAddress[3]) };
    if (iaryIPAddress[0] == 10 || (iaryIPAddress[0] == 192 && iaryIPAddress[1] == 168) || (iaryIPAddress[0] == 172 && (iaryIPAddress[1] >= 16 && iaryIPAddress[1] <= 31)))
    {
        return true;
    }
    else
    {
        // IP Address is "probably" public. This doesn't catch some VPN ranges like OpenVPN and Hamachi.
        return false;
    }
}
于 2014-12-10T12:42:14.250 回答