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 转地址的更多相关文章

  1. TCP/IP协议原理与应用笔记11:TCP/IP中地址与层次关系

    1. 网络中常用的地址: 2. TCP/IP中地址与层次关系 :

  2. 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- ...

  3. php 获取用户的IP、地址、来源

    js方法获取用户的 ip 和 地址 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> ...

  4. 新浪IP库地址

    新浪IP库地址 http://int.dpool.sina.com.cn/iplookup/iplookup.php

  5. 如何根据Ip获取地址信息--Java----待整理完善!!!

    如何根据Ip获取地址信息--Java----待整理完善!!! QQWry.dat数据写入方法: http://www.cnblogs.com/xumingxiang/archive/2013/02/1 ...

  6. PHP 中根据 IP 获取地址

    这里使用的是淘宝 IP 地址库提供的 API 接口. 淘宝 IP 地址库:http://ip.taobao.com/instructions.html API 文档说明:  使用事例: /** * 调 ...

  7. 使用新浪IP库获取IP详细地址

    使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...

  8. ip2region.jar实现ip转地址

    ip转地址 根据ip地址查询出所在地址. GitHub地址 https://github.com/lionsoul2014/ip2region/ pom坐标 <dependency> &l ...

  9. IP的地址的划分

    IP地址的划分是计算机网络中很重要的一个知识点,曾经考过三级,但是长时间不用就会忘掉,现在重新将IP的地址划分整理一遍. 首先IP地址的编址方法经历了三个阶段:分类的IP地址.子网的划分.构成超网 我 ...

  10. java-通过ip获取地址

    添加maven依赖 <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all&l ...

随机推荐

  1. C题 - A+B for Input-Output Practice (II)

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description You ...

  2. java导入导出excel常用操作小结及简单示例

    POI中常用设置EXCEL的操作小结: 操作excel如下 HSSFWorkbook wb = new HSSFWorkbook();  //创建一个webbook,对应一个Excel文件 HSSFS ...

  3. UIControl-IOS开发

    UIControl-IOS开发   UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedControl分段控件.UISlider滑块.UITextField文本 ...

  4. Eclipse 代码自动补全

    使用Eclipse开发Android时,发现代码补全功能太差,完全不像VS似的,输入一个字母就出现代码提示. 下面是在Eclipse中实现类似VS代码补全的方法: ①打开Eclipse->Win ...

  5. Application路径

    根目录:StreamingAssets文件夹 #if UNITY_EDITOR string filepath = Application.dataPath +"/StreamingAsse ...

  6. 【Java】Java Socket编程(1)基本的术语和概念

    计算机程序能够相互联网,相互通讯,这使一切都成为可能,这也是当今互联网存在的基础.那么程序是如何通过网络相互通信的呢?这就是我记录这系列的笔记的原因.Java语言从一开始就是为了互联网而设计的,它为实 ...

  7. Robot Motion

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  8. 想做Android Wear开发?你得先搞明白这四件事

    手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...

  9. 如果你不好好玩printf

    昨天在跟Fiona讨论printf导致程序Crash的问题,就花了点时间看看究竟什么情况下会这样,有兴趣的童鞋可以看看:) 只要是玩过C或者C++的童鞋们,对printf肯定是再熟悉不过了.下面有几个 ...

  10. AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue

    RandomizedQueue 有几个关键点: 1. 选择合适的数据结构,因为需要任意位置删除元素,Linked list 做不到,必须使用resizing arrays. 2. resizing 的 ...