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

推荐订阅源

美团技术团队
B
Blog RSS Feed
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
D
Docker
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
C
Check Point Blog
The Cloudflare Blog
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
量子位
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
T
The Blog of Author Tim Ferriss
博客园 - 【当耐特】
Vercel News
Vercel News
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
博客园 - 司徒正美

博客园 - 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;
        }

    }

}