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