游戏技术文章

获取IP及判断IP是否在区间

时间:2017-2-17 10:58:19  作者:棋牌资源网  来源:棋牌资源网  查看:6627  评论:0
内容摘要:  /// /// 获取客户端IP /// /// public static string GetClien...
 
    /// <summary>
    /// 获取客户端IP
    /// </summary>
    /// <returns></returns>
    public static string GetClientIpAddress()
        {
            var httpContext = HttpContext.Current;
            if (httpContext.Request.ServerVariables == null)
            {
                return null;
            }

            var clientIp = httpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ??
                           httpContext.Request.ServerVariables["REMOTE_ADDR"];

            try
            {
                foreach (var hostAddress in Dns.GetHostAddresses(clientIp))
                {
                    if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return hostAddress.ToString();
                    }
                }

                foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return hostAddress.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                
            }

            return clientIp;
        }


    /// <summary>
    /// ip是否在ip空间内
    /// </summary>
    /// <param name="ip"></param>
    /// <param name="ipSection"></param>
    /// <returns></returns>
    public static Boolean ipExistsInRange(String ip, String ipSection)
    {

        ipSection = ipSection.Trim();

        ip = ip.Trim();

        int idx = ipSection.IndexOf('-');

        String beginIP = ipSection.Substring(0, idx);

        String endIP = ipSection.Substring(idx + 1);

        return getIp2long(beginIP) <= getIp2long(ip) && getIp2long(ip) <= getIp2long(endIP);

    }

    public static long getIp2long(String ip)
    {

        ip = ip.Trim();
         
        String[] ips = ip.Split('.');

        long ip2long = 0L;

        for (int i = 0; i < 4; ++i)
        {

            ip2long = ip2long << 8 | Int64.Parse(ips[i]);

        }

        return ip2long;
    }

    public static long getIp2long2(String ip)
    {

        ip = ip.Trim();

        String[] ips = ip.Split('.');

        long ip1 = Int64.Parse(ips[0]);

        long ip2 = Int64.Parse(ips[1]);

        long ip3 = Int64.Parse(ips[2]);

        long ip4 = Int64.Parse(ips[3]);



        long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;

        return ip2long;

    }
标签:获取IP及判断IP是否在区间 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2021 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved