https://www.ipip.net/download.html

https://github.com/17mon/csharp

IpAndPositionHelper
  public class IpAndPositionHelper
{
#region Resource
public static bool EnableFileWatch = false; private static int offset;
private static uint[] index = new uint[];
private static byte[] dataBuffer;
private static byte[] indexBuffer;
private static long lastModifyTime = 0L;
private static string ipFile;
private static readonly object @lock = new object();
#endregion #region LoadDat
public static void Load(string filename)
{
ipFile = new FileInfo(filename).FullName;
Load();
if (EnableFileWatch)
{
Watch();
}
} private static void Load()
{
lock (@lock)
{
var file = new FileInfo(ipFile);
lastModifyTime = file.LastWriteTime.Ticks;
try
{
dataBuffer = new byte[file.Length];
using (var fin = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
{
fin.Read(dataBuffer, , dataBuffer.Length);
} var indexLength = BytesToLong(dataBuffer[], dataBuffer[], dataBuffer[], dataBuffer[]);
indexBuffer = new byte[indexLength];
Array.Copy(dataBuffer, , indexBuffer, , indexLength);
offset = (int)indexLength; for (var loop = ; loop < ; loop++)
{
index[loop] = BytesToLong(indexBuffer[loop * + ], indexBuffer[loop * + ],
indexBuffer[loop * + ],
indexBuffer[loop * ]);
}
}
catch (Exception ex)
{
throw ex;
}
}
} private static uint BytesToLong(byte a, byte b, byte c, byte d)
{
return ((uint)a << ) | ((uint)b << ) | ((uint)c << ) | d;
} private static void Watch()
{
var file = new FileInfo(ipFile);
if (file.DirectoryName == null) return;
var watcher = new FileSystemWatcher(file.DirectoryName, file.Name) { NotifyFilter = NotifyFilters.LastWrite };
watcher.Changed += (s, e) =>
{
var time = File.GetLastWriteTime(ipFile).Ticks;
if (time > lastModifyTime)
{
Load();
}
};
watcher.EnableRaisingEvents = true;
}
#endregion #region FindIP
public static string[] Find(string ip)
{
lock (@lock)
{
var ips = ip.Split('.');
var ip_prefix_value = int.Parse(ips[]);
long ip2long_value = BytesToLong(byte.Parse(ips[]), byte.Parse(ips[]), byte.Parse(ips[]),
byte.Parse(ips[]));
var start = index[ip_prefix_value];
var max_comp_len = offset - ;
long index_offset = -;
var index_length = -;
byte b = ;
for (start = start * + ; start < max_comp_len; start += )
{
if (
BytesToLong(indexBuffer[start + ], indexBuffer[start + ], indexBuffer[start + ],
indexBuffer[start + ]) >= ip2long_value)
{
index_offset = BytesToLong(b, indexBuffer[start + ], indexBuffer[start + ],
indexBuffer[start + ]);
index_length = 0xFF & indexBuffer[start + ];
break;
}
}
var areaBytes = new byte[index_length];
Array.Copy(dataBuffer, offset + (int)index_offset - , areaBytes, , index_length);
return Encoding.UTF8.GetString(areaBytes).Split('\t');
}
}
#endregion
}
        [TestMethod()]
public void FindTest()
{
IpAndPositionHelper.EnableFileWatch = true; // 默认值为:false,如果为true将会检查ip库文件的变化自动reload数据
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "APP_Data\\tinyipdata_utf8.dat"); //合并目录
IpAndPositionHelper.Load(path);//加载 var infos = IpAndPositionHelper.Find("180.169.67.130");//查询 var result = string.Join("-", infos).TrimEnd('-'); //中国-上海 }

附IP解析文件:

17monipdb4ecdz.zip

17monipdb根据IP获得区域的更多相关文章

  1. 网站定位之---根据IP获得区域

    记得以前做一个培训机构网站时候需要定位,那时候用的搜狐的api,不是很精准. demo:https://github.com/dunitian/LoTCodeBase/tree/master/NetC ...

  2. 超好用的ip归属区域查询

    源码如下: #!/usr/bin/env python#-*-coding:utf-8-*- import requestsfrom bs4 import BeautifulSoupimport re ...

  3. java web获取客户端外网ip和所在区域

    @参考文章1.@参考文章2.@参考文章3.@参考文章4,@之前同事的项目 controller @Controller @RequestMapping("/home") publi ...

  4. 网络通信分享(二):外网ip和内网ip

    一.内网ip包括两类: 1:tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255  172.16.0.0/ ...

  5. TCP/IP详解学习笔记(6)-- IP选路

    1.概述      路由算法是用于获取路由表中的路由项目.它是路由选择协议的核心. 2.路由算法的分类      从路由算法能否随网络的通信量或拓扑自适应的进行调整变化来分,可以分为两类. 静态路由选 ...

  6. TCP/IP——内外网IP+子网掩码作用+PING(网络总结)

    目录: 1.如何区分内网IP和外网IP? 保留字段 2.子网掩码是起什么作用的? 将DNS和IP异或,表示哪段起作用 3.ping到底起什么作用? ping本地.ping远程 下面针对上面三个问题分别 ...

  7. IP选路

    IP选路 1.概述      路由算法是用于获取路由表中的路由项目.它是路由选择协议的核心. 2.路由算法的分类      从路由算法能否随网络的通信量或拓扑自适应的进行调整变化来分,可以分为两类. ...

  8. ip识别运用

    tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/12:172.16.0.0-1 ...

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

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

随机推荐

  1. AndroidManifest 中android:exported

    假设Service等的AndroidManifest中声明为android:exported="false" 则该服务不可以跨进程使用.         Permission De ...

  2. ListView GridViewColumn.CellTemplate

    <ListView.View> <GridView> <GridViewColumn Header="Meaningful Use Objectives&quo ...

  3. SqlServer 在查询结果中如何过滤掉重复数据

    问题背景 在一个多表查询的sql中正常情况下产生的数据都是唯一的,但因为数据库中存在错误(某张表中存在相同的外键ID)导致我这边查询出来的数据就会有重复的问题 下面结果集中UserID:15834存在 ...

  4. Cognos定时刷新RS解决方案

    为了了解医院运营情况,在门诊和挂号业务上用户提出了一个接近实时监测的需求,每隔一段时间需要知道当天的挂号量,等待人数,出诊医生等指标来了解医院当天主要科室的就诊情况,以及医疗资源是否分配得当 一:方案 ...

  5. [Backbone]6. Collections.

    Define a collection: var AppointmentList = Backbone.Collection.extend({model: Appointment}); RESET t ...

  6. ORACLE关于锁表查询的部分SQL

    http://www.cnblogs.com/quanweiru/archive/2012/08/28/2660700.html --查询表空间名称和大小 SELECT UPPER (F.TABLES ...

  7. MVC的Filter应用小结

    一.概念定义 Filter是一种声明式编程方式,在Asp.net MVC中它只能限制于Action(或它的Controller). Filter用途广泛:(1)判断登录与否或用户权限:(2)决策输出缓 ...

  8. 学习中遇到的c++问题,持续更新

    原文请訪问我的博客:http://xiaoshig.sinaapp.com/ 向上取整 使用ceil函数.ceil(x)返回的是大于x的最小整数.如: ceil(2.5) = 3 ceil(-2.5) ...

  9. 解决win10 获取不到ip

    1.检查Dhcp服务是否启动 2.关闭防火墙

  10. navicat oracle library is not loaded

        navicat oracle library is not loaded CreationTime--2018年8月9日19点13分 Author:Marydon 1.情景展示 Navicat ...