zoukankan      html  css  js  c++  java
  • 取IP,判断IP是否合法

     public class IPHelper
        {
            /// <summary>
            /// 得到本机IP
            /// </summary>
            public static string GetLocalIP()
            {
                //本机IP地址
                string strLocalIP = "127.0.0.1";
                //得到计算机名
                string strPcName = Dns.GetHostName();
                //得到本机IP地址数组
                IPHostEntry ipEntry = Dns.GetHostEntry(strPcName);
                //遍历数组
                foreach (IPAddress IPadd in ipEntry.AddressList)
                {
                    //判断当前字符串是否为正确IP地址
                    if (IsRightIP(IPadd.ToString()))
                    {
                        //得到本地IP地址
                        strLocalIP = IPadd.ToString();
                        //结束循环
                        break;
                    }
                }
    
                //返回本地IP地址
                return strLocalIP;
            }
    
            /// <summary>
            /// 判断是否为正确的IP地址
            /// </summary>
            /// <param name="strIPadd">需要判断的字符串</param>
            /// <returns>true = 是 false = 否</returns>
            public static bool IsRightIP(string strIPadd)
            {
                //利用正则表达式判断字符串是否符合IPv4格式
                if (Regex.IsMatch(strIPadd, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
                {
                    //根据小数点分拆字符串
                    string[] ips = strIPadd.Split('.');
                    if (ips.Length == 4 || ips.Length == 6)
                    {
                        //如果符合IPv4规则
                        if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                            //正确
                            return true;
                        //如果不符合
                        else
                            //错误
                            return false;
                    }
                    else
                        //错误
                        return false;
                }
                else
                    //错误
                    return false;
            }
        }
  • 相关阅读:
    做过的笔试题
    (转)32位机器中int的字长
    JS_void()
    JS_增加事件,移除事件,动态元素的增删事件研究
    JS_animate 站在别人的肩膀上
    JS_对象的方法
    JS_Class.extend
    JS_返回值
    JS_eventBind
    JS_应用对象的复制
  • 原文地址:https://www.cnblogs.com/skyblue/p/2395826.html
Copyright © 2011-2022 走看看