IP 转地址
1.需要 QQWry.Dat IP 地址数据库
2辅助类库
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace Utilities
{
/// <summary>
/// 根据IP地址查询所在地
/// </summary>
public class IPScanerHelper
{
#region 私有成员
private string dataPath;
private string ip;
private string country;
private string local; private long firstStartIp = ;
private long lastStartIp = ;
private FileStream objfs = null;
private long startIp = ;
private long endIp = ;
private int countryFlag = ;
private long endIpOff = ;
private string errMsg = null;
#endregion #region 构造函数
public IPScanerHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion #region 公共属性
public string DataPath
{
set { dataPath = value; }
}
public string IP
{
set { ip = value; }
}
public string Country
{
get { return country; }
}
public string Local
{
get { return local; }
}
public string ErrMsg
{
get { return errMsg; }
}
#endregion #region 搜索匹配数据
private int QQwry()
{
string pattern = @"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))";
Regex objRe = new Regex(pattern);
Match objMa = objRe.Match(ip);
if (!objMa.Success)
{
this.errMsg = "IP格式错误";
return ;
} long ip_Int = this.IpToInt(ip);
int nRet = ;
if (ip_Int >= IpToInt("127.0.0.0") && ip_Int <= IpToInt("127.255.255.255"))
{
this.country = "本机内部环回地址";
this.local = "";
nRet = ;
}
else if ((ip_Int >= IpToInt("0.0.0.0") && ip_Int <= IpToInt("2.255.255.255")) || (ip_Int >= IpToInt("64.0.0.0") && ip_Int <= IpToInt("126.255.255.255")) || (ip_Int >= IpToInt("58.0.0.0") && ip_Int <= IpToInt("60.255.255.255")))
{
this.country = "网络保留地址";
this.local = "";
nRet = ;
}
objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read);
try
{
//objfs.Seek(0,SeekOrigin.Begin);
objfs.Position = ;
byte[] buff = new Byte[];
objfs.Read(buff, , );
firstStartIp = buff[] + buff[] * + buff[] * * + buff[] * * * ;
lastStartIp = buff[] * + buff[] * + buff[] * * + buff[] * * * ;
long recordCount = Convert.ToInt64((lastStartIp - firstStartIp) / 7.0);
if (recordCount <= )
{
country = "FileDataError";
objfs.Close();
return ;
}
long rangE = recordCount;
long rangB = ;
long recNO = ;
while (rangB < rangE - )
{
recNO = (rangE + rangB) / ;
this.GetStartIp(recNO);
if (ip_Int == this.startIp)
{
rangB = recNO;
break;
}
if (ip_Int > this.startIp)
rangB = recNO;
else
rangE = recNO;
}
this.GetStartIp(rangB);
this.GetEndIp();
if (this.startIp <= ip_Int && this.endIp >= ip_Int)
{
this.GetCountry();
this.local = this.local.Replace("(我们一定要解放台湾!!!)", "");
}
else
{
nRet = ;
this.country = "未知";
this.local = "";
}
objfs.Close();
return nRet;
}
catch
{
return ;
} }
#endregion #region IP地址转换成Int数据
private long IpToInt(string ip)
{
char[] dot = new char[] { '.' };
string[] ipArr = ip.Split(dot);
if (ipArr.Length == )
ip = ip + ".0";
ipArr = ip.Split(dot); long ip_Int = ;
long p1 = long.Parse(ipArr[]) * * * ;
long p2 = long.Parse(ipArr[]) * * ;
long p3 = long.Parse(ipArr[]) * ;
long p4 = long.Parse(ipArr[]);
ip_Int = p1 + p2 + p3 + p4;
return ip_Int;
}
#endregion #region int转换成IP
private string IntToIP(long ip_Int)
{
long seg1 = (ip_Int & 0xff000000) >> ;
if (seg1 < )
seg1 += 0x100;
long seg2 = (ip_Int & 0x00ff0000) >> ;
if (seg2 < )
seg2 += 0x100;
long seg3 = (ip_Int & 0x0000ff00) >> ;
if (seg3 < )
seg3 += 0x100;
long seg4 = (ip_Int & 0x000000ff);
if (seg4 < )
seg4 += 0x100;
string ip = seg1.ToString() + "." + seg2.ToString() + "." + seg3.ToString() + "." + seg4.ToString(); return ip;
}
#endregion #region 获取起始IP范围
private long GetStartIp(long recNO)
{
long offSet = firstStartIp + recNO * ;
//objfs.Seek(offSet,SeekOrigin.Begin);
objfs.Position = offSet;
byte[] buff = new Byte[];
objfs.Read(buff, , ); endIpOff = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * ;
startIp = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * + Convert.ToInt64(buff[].ToString()) * * * ;
return startIp;
}
#endregion #region 获取结束IP
private long GetEndIp()
{
//objfs.Seek(endIpOff,SeekOrigin.Begin);
objfs.Position = endIpOff;
byte[] buff = new Byte[];
objfs.Read(buff, , );
this.endIp = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * + Convert.ToInt64(buff[].ToString()) * * * ;
this.countryFlag = buff[];
return this.endIp;
}
#endregion #region 获取国家/区域偏移量
private string GetCountry()
{
switch (this.countryFlag)
{
case :
case :
this.country = GetFlagStr(this.endIpOff + );
this.local = ( == this.countryFlag) ? " " : this.GetFlagStr(this.endIpOff + );
break;
default:
this.country = this.GetFlagStr(this.endIpOff + );
this.local = this.GetFlagStr(objfs.Position);
break;
}
return " ";
}
#endregion #region 获取国家/区域字符串
private string GetFlagStr(long offSet)
{
int flag = ;
byte[] buff = new Byte[];
while ( == )
{
//objfs.Seek(offSet,SeekOrigin.Begin);
objfs.Position = offSet;
flag = objfs.ReadByte();
if (flag == || flag == )
{
objfs.Read(buff, , );
if (flag == )
{
this.countryFlag = ;
this.endIpOff = offSet - ;
}
offSet = Convert.ToInt64(buff[].ToString()) + Convert.ToInt64(buff[].ToString()) * + Convert.ToInt64(buff[].ToString()) * * ;
}
else
{
break;
}
}
if (offSet < )
return " ";
objfs.Position = offSet;
return GetStr();
}
#endregion #region GetStr
private string GetStr()
{
byte lowC = ;
byte upC = ;
string str = "";
byte[] buff = new byte[];
while ( == )
{
lowC = (Byte)objfs.ReadByte();
if (lowC == )
break;
if (lowC > )
{
upC = (byte)objfs.ReadByte();
buff[] = lowC;
buff[] = upC;
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
str += enc.GetString(buff);
}
else
{
str += (char)lowC;
}
}
return str;
}
#endregion #region 获取IP地址
public string IPLocation()
{
this.QQwry();
return this.country + this.local;
}
public string IPLocation(string dataPath, string ip)
{
this.dataPath = dataPath;
this.ip = ip;
this.QQwry();
return this.country + this.local;
}
#endregion
}
}
3.测试
string IPAddress = NetHelper.GetIPAddress();
IPScanerHelper objScan = new IPScanerHelper();
objScan.IP = IPAddress;
objScan.DataPath = Server.MapPath("~/Resource/IPScaner/QQWry.Dat");
string IPAddressName =objScan.IPLocation();
IP 转地址的更多相关文章
- TCP/IP协议原理与应用笔记11:TCP/IP中地址与层次关系
1. 网络中常用的地址: 2. TCP/IP中地址与层次关系 :
- IP V4地址分类
IP V4地址 共分为五类: A类地址范围:1.0.0.1---126.255.255.254 B类地址范围:128.0.0.1---191.255.255.254 C类地址范围:192.0.0.1- ...
- php 获取用户的IP、地址、来源
js方法获取用户的 ip 和 地址 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> ...
- 新浪IP库地址
新浪IP库地址 http://int.dpool.sina.com.cn/iplookup/iplookup.php
- 如何根据Ip获取地址信息--Java----待整理完善!!!
如何根据Ip获取地址信息--Java----待整理完善!!! QQWry.dat数据写入方法: http://www.cnblogs.com/xumingxiang/archive/2013/02/1 ...
- PHP 中根据 IP 获取地址
这里使用的是淘宝 IP 地址库提供的 API 接口. 淘宝 IP 地址库:http://ip.taobao.com/instructions.html API 文档说明: 使用事例: /** * 调 ...
- 使用新浪IP库获取IP详细地址
使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...
- ip2region.jar实现ip转地址
ip转地址 根据ip地址查询出所在地址. GitHub地址 https://github.com/lionsoul2014/ip2region/ pom坐标 <dependency> &l ...
- IP的地址的划分
IP地址的划分是计算机网络中很重要的一个知识点,曾经考过三级,但是长时间不用就会忘掉,现在重新将IP的地址划分整理一遍. 首先IP地址的编址方法经历了三个阶段:分类的IP地址.子网的划分.构成超网 我 ...
- java-通过ip获取地址
添加maven依赖 <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all&l ...
随机推荐
- Tornado 的教材
Tornado 的教材 作者:杨昆链接:https://www.zhihu.com/question/19707966/answer/12731684来源:知乎著作权归作者所有,转载请联系作者获得授权 ...
- instancetype 与 id for Objective-C
instancetype.id.NSObject的区别 - simalone 1.instancetype只能用于方法的返回类型,而id用处和NSObject *类似. 2.instancetyp ...
- win7如何开启和关闭超级管理员账户
激活命令: net user administrator /active:yes 关闭命令: net user administrator /active:no
- google chrome中如何删除一条输入网址提示
在google chrome中网站栏输入字母的时候会出现网址的提示,如下图: 之前遇到个问题,不知道之前打错了www.baidu.com为wwww.baidu.com(也会跳转到百度)导致一输入“w” ...
- 转:Ubuntu12.04 LTS 使用心得-开机挂载其他分区
1.在/media目录下创建好用来关联你要挂载的分区的文件夹(相当于一个虚拟目录/挂载点,链接/映射到你要挂载的盘符去) 我要挂载4个分区,所以创建了四个挂载点,名字随便取,只要你自己认的出来哪个对应 ...
- 利用多线程资源竞争技术上传shell
通过多线程资源竞争的手段同时上传两个头像,就可以在Apache+Rails环境下实现远程代码执行.这并不是天方夜谭,同时我相信许多文件上传系统都会有这个漏洞……这是一个非常有趣的安全实验,一起来看看吧 ...
- ARM Cortex M3系列GPIO口介绍(工作方式探讨)
一.Cortex M3的GPIO口特性 在介绍GPIO口功能前,有必要先说明一下M3的结构框图,这样能够更好理解总线结构和GPIO所处的位置. Cortex M3结构框图 从图中可以看出 ...
- sphinx插入代码
示例的Python源代码或者交互界面都可以使用标准reST模块实现.在正常段落后面跟着 :: 开始,再加上适当缩进. 交互界面需包含提示及Python代码的输出. 交互界面没有特别的标记. 在最后一行 ...
- oracle 表查询(2)
使用逻辑操作符号 问题:查询工资高于500 或者是岗位为MANAGER 的雇员,同时还要满足他们的姓名首字母为大写的J? or job = 'MANAGER') and ename LIKE 'J%' ...
- kafka在zookeeper中的存储结构
参考site:http://kafka.apache.org/documentation.html#impl_zookeeper 1.zookeeper客户端相关命令 在确保zookeeper服务启动 ...