惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
I
InfoQ
F
Fortinet All Blogs
J
Java Code Geeks
Last Week in AI
Last Week in AI
美团技术团队
大猫的无限游戏
大猫的无限游戏
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件

博客园 - Caviare

前台优化法则(YSlow) ASP.NET MVC源代码 启用IIS的Gzip压缩功能 web.sitemap Dynamic Generation SQL字符正则匹配 委托应用 WorkBook操作实例 迭代器和排序基本使用 Bertrand Meyer提出的设计模式的原则 - Caviare - 博客园 Web.cofig详解[转] UpdatePanel Control [转老赵博客] .Net环境下的缓存技术介绍 (转) [转]webservice和remoting在分布式程序中的应用 sql2005备份在sql2000中恢复 Excel WorkBook操作例 温故知新-Excel操作类 SQL collation设置 一张类的访问权限图,帮您加深概念 [转]您可能不知道的Asp.Net2.0技巧
判断大陆IPAddress
Caviare · 2007-11-05 · via 博客园 - Caviare

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace IPInfoService
{
    
public class AddressService
    
{
        
public List<IPArea> listIP = new List<IPArea>();

        
/// <summary>
        
/// 填充大陆IP代码段列表
        
/// </summary>

        public AddressService()
        
{
            
string iPDBPath = @"D:\ApexLog\eTrade\Clicks\ipDB\country-ipv4.lst";//Down loaded from "http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst"
            StreamReader srIp = new StreamReader(iPDBPath);
            
while (!srIp.EndOfStream)
            
{
                
string strLine = srIp.ReadLine();
                
int index = strLine.IndexOf("cn");
                
if (index != -1)
                
{
                    
string[] strArr = strLine.Split(' ');

                    listIP.Add(
new IPArea(strArr[0], strArr[2]));
                }

            }

            srIp.Close();
        }


        
/// <summary>
        
/// 该IP是否来自中国大陆
        
/// </summary>
        
/// <param name="ipAddress"></param>
        
/// <returns></returns>

        public bool ScopeIncludingIpAddress(string ipAddress)
        
{
            
long intCurrentIP = IpToInt(ipAddress);
            
for (int i = 0; i < listIP.Count; i++)
            
{
                
if (IpToInt(listIP[i].strIpStart) <= intCurrentIP && intCurrentIP <= IpToInt(listIP[i].strIpEnd))
                
{
                    
return true;
                }

            }

            
return false;
        }


        
/// <summary>
        
/// IP地址转换成Int数据
        
/// </summary>
        
/// <param name="ip"></param>
        
/// <returns></returns>

        private long IpToInt(string ip)
        
{
            
char[] dot = new char[] '.' };
            
string[] ipArr = ip.Split(dot);
            
if (ipArr.Length == 3)
                ip 
= ip + ".0";
            ipArr 
= ip.Split(dot);

            
long ip_Int = 0;
            
long p1 = long.Parse(ipArr[0]) * 256 * 256 * 256;
            
long p2 = long.Parse(ipArr[1]) * 256 * 256;
            
long p3 = long.Parse(ipArr[2]) * 256;
            
long p4 = long.Parse(ipArr[3]);
            ip_Int 
= p1 + p2 + p3 + p4;
            
return ip_Int;
        }


    }


    
public class IPArea
    
{
        
public string strIpStart;
        
public string strIpEnd;
        
public IPArea(string strStart,string strEnd)
        
{
            
this.strIpEnd = strEnd;
            
this.strIpStart = strStart;
        }

    }

}