简体   繁体   English

与C#中的isSiteLocalAddress等效

[英]Equivalent of isSiteLocalAddress in C#

在Java中,有一个方法inetaddress.isSiteLocalAddrress可以确定IP地址是否与站点的IP地址在同一范围内,我们在C#中是否有任何等效方法?

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM