///<summary>
/// 提供从纯真IP数据库搜索IP信息的方法;
///</summary>
public class IPSelect
{
FileStream ipFile;
long ip;
public string ipfilePath;
///<summary>
/// 构造函数
///</summary>
///<param name="ipfilePath">纯真IP数据库路径</param>
public IPSelect()
{
ipfilePath = "纯真IP数据库路径";//TextUtility.GetFullPath( Utility.GetIPDbFilePath );
} ///<summary>
/// 地理位置,包括国家和地区
///</summary>
public struct IPLocation
{
public string country, area;
} ///<summary>
/// 获取指定IP所在地理位置实体
///</summary>
///<param name="strIP">要查询的IP地址</param>
///<returns></returns>
public string GetIPLocation( string strIP )
{
if (!File.Exists(ipfilePath))
{
return "";
}
ip = IPToLong( strIP ); //将字符串形式的IP转换位long
IPLocation loc = new IPLocation();
try
{
ipFile = new FileStream(ipfilePath, FileMode.Open, FileAccess.Read); //文件流
long[] ipArray = BlockToArray(ReadIPBlock()); //
long offset = SearchIP(ipArray, , ipArray.Length - ) * + ;
ipFile.Position += offset; //跳过起始IP
ipFile.Position = ReadLongX() + ; //跳过结束IP int flag = ipFile.ReadByte(); //读取标志
if (flag == ) //表示国家和地区被转向
{
ipFile.Position = ReadLongX();
flag = ipFile.ReadByte(); //再读标志
}
long countryOffset = ipFile.Position;
loc.country = ReadString(flag); if (flag == )
{
ipFile.Position = countryOffset + ;
}
flag = ipFile.ReadByte();
loc.area = ReadString(flag); ipFile.Close();
ipFile = null;
}
catch
{
return "";
}
return loc.country + loc.area;
}
///<summary>
/// 将字符串形式的IP转换位long
///</summary>
///<param name="strIP"></param>
///<returns></returns>
public long IPToLong( string strIP )
{
byte[] ip_bytes = new byte[];
string[] strArr = strIP.Split( new char[] { '.' } );
for( int i = ; i < ; i++ )
{
ip_bytes[i] = byte.Parse( strArr[ - i] );
}
return BitConverter.ToInt64( ip_bytes, );
}
///<summary>
/// 将索引区字节块中的起始IP转换成Long数组
///</summary>
///<param name="ipBlock"></param>
long[] BlockToArray( byte[] ipBlock )
{
long[] ipArray = new long[ipBlock.Length / ];
int ipIndex = ;
byte[] temp = new byte[];
for( int i = ; i < ipBlock.Length; i += )
{
Array.Copy( ipBlock, i, temp, , );
ipArray[ipIndex] = BitConverter.ToInt64( temp, );
ipIndex++;
}
return ipArray;
}
///<summary>
/// 从IP数组中搜索指定IP并返回其索引
///</summary>
///<param name="ipArray">IP数组</param>
///<param name="start">指定搜索的起始位置</param>
///<param name="end">指定搜索的结束位置</param>
///<returns></returns>
int SearchIP( long[] ipArray, int start, int end )
{
int middle = ( start + end ) / ;
if( middle == start )
return middle;
else if( ip < ipArray[middle] )
return SearchIP( ipArray, start, middle );
else
return SearchIP( ipArray, middle, end );
}
///<summary>
/// 读取IP文件中索引区块
///</summary>
///<returns></returns>
byte[] ReadIPBlock()
{
long startPosition = ReadLongX( );
long endPosition = ReadLongX( );
long count = ( endPosition - startPosition ) / + ;//总记录数
ipFile.Position = startPosition;
byte[] ipBlock = new byte[count * ];
ipFile.Read( ipBlock, , ipBlock.Length );
ipFile.Position = startPosition;
return ipBlock;
}
///<summary>
/// 从IP文件中读取指定字节并转换位long
///</summary>
///<param name="bytesCount">需要转换的字节数,主意不要超过8字节</param>
///<returns></returns>
long ReadLongX( int bytesCount )
{
byte[] _bytes = new byte[];
ipFile.Read( _bytes, , bytesCount );
return BitConverter.ToInt64( _bytes, );
}
///<summary>
/// 从IP文件中读取字符串
///</summary>
///<param name="flag">转向标志</param>
///<returns></returns>
string ReadString( int flag )
{
if( flag == || flag == )//转向标志
ipFile.Position = ReadLongX( );
else
ipFile.Position -= ; List<byte> list = new List<byte>();
byte b = (byte)ipFile.ReadByte();
while( b > )
{
list.Add( b );
b = (byte)ipFile.ReadByte();
}
return Encoding.Default.GetString( list.ToArray() );
}
}
        /// <summary>
/// 根据IP的地理位置
/// </summary>
/// <param name="IP">IP</param>
/// <returns>地理位置</returns>
protected string GetAddressWithIP(string IP)
{
IPSelect IPSelect = new IPSelect();
if (string.IsNullOrEmpty(IP))
{
IP = "127.0.0.1";
}
return IPSelect.GetIPLocation(IP);
}

纯真IP数据库格式文档在【IP数据库文件

根据IP地址获得地理位置的更多相关文章

  1. python36异步任务 获取ip地址的地理位置

    # encoding: utf-8 import asyncio import requests import functools url = "http://ip.zxinc.org/ap ...

  2. Linux下Python获取IP地址

    <lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...

  3. PHP 获取IP地址位置信息「聚合数据API」

    聚合数据 提供了[查询IP所属区域]的服务接口,只需要以 GET 请求的方式向 API 传入 IP地址 和 APPKEY 即可获得查询结果. 这里的难点主要在于如何通过PHP获取客户端IP地址,以及如 ...

  4. MFC C++ 获取外网IP地址

    #include <afxinet.h> //GB2312 转换成 Unicode wchar_t* GB2312ToUnicode(const char* szGBString) { U ...

  5. 腾讯新浪通过IP地址获取当前地理位置(省份)的接口

    腾讯新浪通过IP地址获取当前地理位置(省份)的接口  腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array(" ...

  6. 根据ip地址从第三方接口获取详细的地理位置

    最近项目某个功能需要根据ip地址从第三方接口获取详细的地理位置,从网上找了很多例子,主要接口有新浪的,淘宝的,腾讯的.试了淘宝的,如果是数量级小的还可以,如果数量级达到上十万级就速度慢了,会导致系统崩 ...

  7. php获取ip地址所在的地理位置的实现

    1,通过腾讯或者新浪提供的接口来获取(新浪和腾讯类似) <?php     function getIPLocation($queryIP){      $url = 'http://ip.qq ...

  8. linux c++ curl 根据IP地址获得当前网络的所在的地理位置

    注意: 可能每个电脑的默认中文编码格式不同,有时会出现乱码,需要对返回内容进行编码转换,或者换成可指定编码格式的接口.如  搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/c ...

  9. 通过IP地址定位准确的地理位置

    事情的经过时这样的: 朋友发来一封QQ邮件原文,询问里面显示的IP地址是不是真是的IP地址.然后,我就解锁了一项新技能:通过IP地址定位准确的地理位置 在这里收藏一下这个网址:http://www.8 ...

随机推荐

  1. CentOS6.5安装MySQL5.7详细教程

    注:文中所写的安装过程均在CentOS6.5 x86下通过测试 主要参考博文: https://segmentfault.com/a/1190000003049498 http://www.th7.c ...

  2. 挺有意思的HBase日志+Splunk

    如题,转载自: http://hi.baidu.com/harry_lime/item/10cf2c174853c7ea39cb3042 如何模拟拔盘操作 Linux has a nifty way ...

  3. Maven Jrebel 多模块热部署方案

    近期在构建maven多模块项目时,发现web module依赖的其它模块,每次都要clean install成一个jar包,然后运行web module才能加载. 本生jrebel是配置在了web m ...

  4. nginx 配置日志

    http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $r ...

  5. 解题报告 HDU1789 Doing Homework again

    Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  6. POJ3318【随机化算法挺有意思】

    题目链接:http://poj.org/problem?id=3318 http://meizhe143.blog.163.com/blog/static/3893836200710299512136 ...

  7. 如何使用W5300实现ADSL连接(一)

    在介绍W5300连接ADSL之前,先给大家简单介绍一下WIZnet W5300这款芯片. W5300是WIZnet公司的一款单芯片器件,采用0.18μmCMOS工艺,内部集成10/100M以太网控制器 ...

  8. Currency System in Geraldion (Codeforces 560A)

    A  Currency System in Geraldion Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64 ...

  9. [Swust OJ 1094]--中位数(巧用set,堆排序)

    题目链接:http://acm.swust.edu.cn/problem/1094/ Time limit(ms): 1000 Memory limit(kb): 32768   中位数(又称中值,英 ...

  10. Reapter 添加删除按钮

    repeater中的删除按钮和datagrid下的删除在实现上,还是有一定的区别的,由于repeater在客户端生成的html代码是非常干净的,所以特别受到众多web2.0网站的欢迎(不像datagr ...