且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确定是否在私IP地址?

更新时间:2023-02-22 11:50:26

一个更详细的回应是在这里:

A more detailed response is here:

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