IP和端口判断(C#)

来源:互联网 发布:简谱识别软件 编辑:程序博客网 时间:2024/05/16 06:07

#region ip和port 有效性判断
        /// <summary>
        /// ip的有效性
        /// </summary>
        /// <param name="ip">IP</param>
        /// <returns></returns>
        public bool IP_YN(String ip)
        {
            bool YN = false;
            if (System.Text.RegularExpressions.Regex.IsMatch(ip, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
            {

                string[] ips = ip.Split('.');
                if (ips.Length == 4)
                    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)
                    {
                        YN = true;
                        IniWriteValue("area", "ip", ip);
                    }
            }

            return YN;
        }
        /// <summary>
        /// port有效性
        /// </summary>
        /// <param name="str">端口</param>
        /// <returns></returns>
        public bool PORT_YN(String str)
        {
            bool YN = true;
            if (str.Length == 0)
            {
                YN = false;
            }
            else//没有非数字字符
            {
                for (int i = 0; i < str.Length; i++)
                    if (!char.IsNumber(str, i))
                        YN = false;
            }
            if (YN)//介于0-65535之间
            {
                if ((int.Parse(str) > 0) && (int.Parse(str) < 65535))
                {
                    YN = true;
                    IniWriteValue("area", "port", str);
                }
            }
            return YN;


        }

#endregion
原创粉丝点击