判断大陆IPAddress

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;
        }

    }

}

转载于:https://www.cnblogs.com/caviare/archive/2007/11/05/949783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值