///<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. 【转载】CentsOS系统inotify实时监控服务器文件(夹)定制事件处理程序

    原始博文和参考博文 1.CentsOS系统inotify实时监控服务器文件 2.Linux中让进程在后台运行的方法 3.linux inotify 监控文件系统事件 非常好 方法一 说明: 服务器系统 ...

  2. 【转载】国内网站博客数据统计选免费Google Analytics还是百度统计

    [转载]国内网站博客数据统计选免费Google Analytics还是百度统计 Google Analytics谷歌统计是我用的第一个网站统计工具,当然现在也一直在用.Google Analytics ...

  3. Django问题汇总

    Q1.运行python manage.py runserver后,出现如下错误(CentOS6.5x86,Python2.7.12,Django1.10): django.core.exception ...

  4. ASP.NET jQuery 随笔 显示CheckBoxList成员选中的内容

    通过jQuery来获取CheckBoxList成员内容. <%@ Page Language="C#" AutoEventWireup="true" Co ...

  5. STL front() ,back()和begin(),end()区别

    首先看看vector里面的: reference front(); const_reference front() const; queue里面的: value_type& front(); ...

  6. Co-prime(容斥)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. RGB,CMYK,HSB各种颜色表示的转换 C#语言

    Introduction Why an article on "colors"? It's the same question I asked myself before writ ...

  8. Web Deploy发布网站一条龙解决方案

    Web Deploy工具对于ASP.NET开发人员来说一定不陌生,没有用过也经常见到,Web Deploy发布十分方便而且在发布时会帮助用户检验发布文件的正确性.接下来介绍一下基础使用. 第一步:安装 ...

  9. 枚举最短路径+SPFA

    Harry Potter and the Final Battle Submit Status Description The final battle is coming. Now Harry Po ...

  10. pChart图表插件使用

    <?php /* 柱形图 */ #引入库文件 include("../class/pData.class.php"); include("../class/pDra ...