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 ...
随机推荐
- cygwin编译ffmpeg移植到android平台问题集锦
编译环境: windows xp Cygwin 1.1.3.1 NDK r9 1.提示各种command not found 比如 ./config.sh: line 6: $'--arch=arm\ ...
- 在linux下实现UBOOT的TFTP下载功能
一.环境 1.条件 软件:虚拟机下linux(本文涉及到的是Ubuntu12.0.4). linux下的串口助手(例如minicom)或windows下的串口助手(例如超级终端.SecureCRT) ...
- windows server 2012安装.net framework3.5失败解决方法
1.点击开始运行,输入 gpedit.msc,打开“组策略”2.“计算机配置”---“管理模板”---“windows 组件”---“windows 更新”,然后双击“指定 Intranet Micr ...
- js的引用顺序
注意:Bootstrap中的JS插件依赖于JQuery,因此JQuery要在Bootstrap之前引用!!! 把JS文件引用放入body的最下面,是为了使js在网页全部加载完后才起作用,比如你的js里 ...
- oracle 查询dblink的方法
oracle 查询dblink的方法: SYSDBA登录, sys登录 SELECT * FROM SYS.link$;
- XSHELL和XFTP,亲兄弟啊。
XSHELL在LINUX和WINDOWS之间传输文件时不力啊.又对FTP不灵活的时候,XFTP就可以出场了. 只要登陆进XSHELL就可以操作了.并且XFTP客户端和命令行可以灵活配置选择. 然后,玩 ...
- 见过NTP服务,没见过网络流量到200M左右的NTP服务
XXX,看来可能是NTP.CONF的文件配置错误所致了. 附上一段查看网络流量的SHELL.(好像只针对ETH0,如果要看其它的,还需要修改) #!/bin/bash typeset in_old d ...
- 模拟I2C从机
模拟I2C主机的比较多,但是从机相对主机而言要难很多,这个供大家借鉴. 这个从机程序支持主机对它的随机写和随机读,连续读和连续写没做,有兴趣的可以完善下,呵呵. //Microcontrol CODE ...
- android 读取SD卡文件
SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件.因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android ...
- BestCoder Round #51 (div.2)
明显是无良心的数学round= = 1000 Zball in Tina Town #include<iostream> #include<cstdio> #include&l ...