zoukankan      html  css  js  c++  java
  • 2019-9-2-win10-uwp-判断本地ip

    title author date CreateTime categories
    win10 uwp 判断本地ip
    lindexi
    2019-09-02 12:57:38 +0800
    2018-2-13 17:23:3 +0800
    Win10 UWP

    本文主要:如何判断一个IP是本地IP

    对于本地 127.0.0.1 就是一个内部IP,之外,还有10.0.0.0/24172.16.0.0/16192.168.0.0/16169.254.0.0/16

    判断是不是本地,首先判断是不是127.0.0.1

            private bool IsPrivateIP(IPAddress myIPAddress)
            {
                if (IPAddress.IsLoopback(myIPAddress))
                {
                    return true;
                }
            }
    

    判断是不是10.0.0.0/24

                byte[] ipBytes = myIPAddress.GetAddressBytes();
                // 10.0.0.0/24 
                if (ipBytes[0] == 10)
                {
                    return true;
                }
    

    判断172.16.0.0/16

                    if (ipBytes[0] == 172 && ipBytes[1] == 16)
                    {
                        return true;
                    }
    

    判断192.168.0.0/16

                    if (ipBytes[0] == 192 && ipBytes[1] == 168)
                    {
                        return true;
                    }
    

    判断169.254.0.0/16

                    if (ipBytes[0] == 169 && ipBytes[1] == 254)
                    {
                        return true;
                    }
    

    源代码:

            /// <summary>
            /// 判断私有ip
            /// </summary>
            /// <param name="myIPAddress"></param>
            /// <returns></returns>
            private bool IsPrivateIP(IPAddress myIPAddress)
            {
                if (IPAddress.IsLoopback(myIPAddress))
                {
                    return true;
                }
                if (myIPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    byte[] ipBytes = myIPAddress.GetAddressBytes();
                    // 10.0.0.0/24 
                    if (ipBytes[0] == 10)
                    {
                        return true;
                    }
                    // 172.16.0.0/16
                    else if (ipBytes[0] == 172 && ipBytes[1] == 16)
                    {
                        return true;
                    }
                    // 192.168.0.0/16
                    else if (ipBytes[0] == 192 && ipBytes[1] == 168)
                    {
                        return true;
                    }
                    // 169.254.0.0/16
                    else if (ipBytes[0] == 169 && ipBytes[1] == 254)
                    {
                        return true;
                    }
                }
                return false;
            }
    
  • 相关阅读:
    asp.net 网页标题、关键字、描述
    星级评分jQuery插件
    以jquery为基础的星星评分
    投票系统显示结果jQuery插件
    JAVASCRIPT模拟模式对话窗口
    Repeater 嵌套代码
    window服务程序安装卸载批处理文件
    c#window程序开发入门系列自学笔记
    jquery 模式对话框改进版
    php的正则表达式完全手册
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085479.html
Copyright © 2011-2022 走看看