.NET读取QQWry.Dat 纯真版ip数据库格式数据源

  1. 最新版QQWry.dat下载地址:<a href="http://download.csdn.net/detail/fwj380891124/4385554">http://download.csdn.net/detail/fwj380891124/4385554</a> 
最新版QQWry.dat下载地址:http://download.csdn.net/detail/fwj380891124/4385554
  1. 外部调用: 
外部调用:
  1. //测试地址搜索#region 测试地址搜索  
       //测试地址搜索#region 测试地址搜索 
  1.         IPScaner objScan = new IPScaner(); 
  2.         string ip = Request.UserHostAddress.ToString(); 
  3.         objScan.DataPath = @"D:\\webwz\\zyue.cn\\App_data\\QQWry.Dat"
  4.         objScan.IP = ip; 
  5.         string addre = objScan.IPLocation(); 
  6.         string err = objScan.ErrMsg; 
  7.         Response.Write(addre);//国家  
  8.         Response.Write(err);//地区  
  9.         类: 
  10. /******************************************************************
  11. ** File Name:IPScaner.cs
  12. ** Copyright (c) 2004-2005 PPTech Studio(PPTech.Net)
  13. ** Creater:Rexsp(MSN:yubo@x263.net)
  14. ** Create Date:2004-12-27 20:10:28
  15. ** Modifier:
  16. ** Modify Date:
  17. ** Description:to scan the ip location from qqwry.dat
  18. ** Version: IPScaner 1.0.0
  19. ******************************************************************/  
  20. using System;  
  21. using System.IO;  
  22. using System.Collections;  
  23. using System.Text;  
  24. using System.Text.RegularExpressions;  
  25. namespace PPTech.WebSite.BusinessRules  
  26. {  
  27.     /**//// <summary>  
  28.     /// to scan the ip location from qqwry.dat  
  29.     /// </summary>  
  30.     public class IPScaner  
  31.     {  
  32.         //私有成员#region 私有成员  
  33.         private string dataPath;  
  34.         private string ip;  
  35.         private string country;  
  36.         private string local;  
  37.  
  38.         private long firstStartIp=0;  
  39.         private long lastStartIp=0;  
  40.         private FileStream objfs = null;  
  41.         private long startIp=0;  
  42.         private long endIp=0;  
  43.         private int countryFlag=0;  
  44.         private long endIpOff=0;  
  45.         private string errMsg=null
  46.  
  47.  
  48.         //构造函数#region 构造函数  
  49.         public IPScaner()  
  50.         {  
  51.             //  
  52.             // TODO: 在此处添加构造函数逻辑  
  53.             //  
  54.         } 
  55.  
  56.  
  57.         //公共属性#region 公共属性  
  58.         public string DataPath  
  59.         {  
  60.             set{dataPath=value;}  
  61.         }  
  62.         public string IP  
  63.         {  
  64.             set{ip=value;}  
  65.         }  
  66.         public string Country  
  67.         {  
  68.             get{return country;}  
  69.         }  
  70.         public string Local  
  71.         {  
  72.             get{return local;}  
  73.         }  
  74.         public string ErrMsg  
  75.         {  
  76.             get{return errMsg;}  
  77.         } 
  78.  
  79.  
  80.         //搜索匹配数据#region 搜索匹配数据  
  81.         private int QQwry()  
  82.         {  
  83.             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]))";  
  84.             Regex objRe = new Regex(pattern);  
  85.             Match objMa = objRe.Match(ip);  
  86.             if(!objMa.Success)  
  87.             {  
  88.                 this.errMsg="IP格式错误";  
  89.                 return 4;  
  90.             }  
  91.  
  92.             long ip_Int = this.IpToInt(ip);  
  93.             int nRet=0;  
  94.             if(ip_Int>=IpToInt("127.0.0.0")&&ip_Int<=IpToInt("127.255.255.255"))  
  95.             {  
  96.                 this.country="本机内部环回地址";  
  97.                 this.local="";  
  98.                 nRet=1;  
  99.             }  
  100.             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")))  
  101.             {  
  102.                 this.country="网络保留地址";  
  103.                 this.local="";  
  104.                 nRet=1;  
  105.             }  
  106.             objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read);  
  107.             try  
  108.             {  
  109.                 //objfs.Seek(0,SeekOrigin.Begin); 
  110.                 objfs.Position=0;  
  111.                 byte[] buff =new Byte[8] ;  
  112.                 objfs.Read(buff,0,8);  
  113.                 firstStartIp=buff[0]+buff[1]*256+buff[2]*256*256+buff[3]*256*256*256;  
  114.                 lastStartIp=buff[4]*1+buff[5]*256+buff[6]*256*256+buff[7]*256*256*256;  
  115.                 long recordCount=Convert.ToInt64((lastStartIp-firstStartIp)/7.0);  
  116.                 if(recordCount<=1)  
  117.                 {  
  118.                     country="FileDataError";  
  119.                     objfs.Close();  
  120.                     return 2;  
  121.                 }  
  122.                 long rangE=recordCount;  
  123.                 long rangB=0;  
  124.                 long recNO=0;  
  125.                 while(rangB<rangE-1)  
  126.                 {  
  127.                     recNO=(rangE+rangB)/2;  
  128.                     this.GetStartIp(recNO);  
  129.                     if(ip_Int==this.startIp)  
  130.                     {  
  131.                         rangB = recNO;  
  132.                         break;  
  133.                     }  
  134.                     if(ip_Int>this.startIp)  
  135.                         rangB=recNO;  
  136.                     else  
  137.                         rangE=recNO;  
  138.                 }  
  139.                 this.GetStartIp(rangB);  
  140.                 this.GetEndIp();  
  141.                 if(this.startIp<=ip_Int&&this.endIp>=ip_Int)  
  142.                 {  
  143.                     this.GetCountry();  
  144.                     this.local=this.local.Replace("(我们一定要解放台湾!!!)","");  
  145.                 }  
  146.                 else  
  147.                 {  
  148.                     nRet=3;  
  149.                     this.country="未知";  
  150.                     this.local="";  
  151.                 }  
  152.                 objfs.Close();  
  153.                 return nRet;  
  154.             }  
  155.             catch  
  156.             {  
  157.                 return 1;  
  158.             }  
  159.  
  160.         }  
  161.  
  162.        // IP地址转换成Int数据#region IP地址转换成Int数据  
  163.         private long IpToInt(string ip)  
  164.         {  
  165.             char[] dot =new char[]{'.'};  
  166.             string [] ipArr = ip.Split(dot);  
  167.             if(ipArr.Length==3)  
  168.                 ip=ip+".0";  
  169.             ipArr=ip.Split(dot);  
  170.  
  171.             long ip_Int=0;  
  172.             long p1=long.Parse(ipArr[0])*256*256*256;  
  173.             long p2=long.Parse(ipArr[1])*256*256;  
  174.             long p3=long.Parse(ipArr[2])*256;  
  175.             long p4=long.Parse(ipArr[3]);  
  176.             ip_Int=p1+p2+p3+p4;  
  177.             return ip_Int;  
  178.         }  
  179.  
  180.         //int转换成IP#region int转换成IP  
  181.         private string IntToIP(long ip_Int)  
  182.         {  
  183.             long seg1=(ip_Int&0xff000000)>>24;  
  184.             if(seg1<0)  
  185.                 seg1+=0x100;  
  186.             long seg2=(ip_Int&0x00ff0000)>>16;  
  187.             if(seg2<0)  
  188.                 seg2+=0x100;  
  189.             long seg3=(ip_Int&0x0000ff00)>>8;  
  190.             if(seg3<0)  
  191.                 seg3+=0x100;  
  192.             long seg4=(ip_Int&0x000000ff);  
  193.             if(seg4<0)  
  194.                 seg4+=0x100;  
  195.             string ip=seg1.ToString()+"."+seg2.ToString()+"."+seg3.ToString()+"."+seg4.ToString();  
  196.  
  197.             return ip;  
  198.         }  
  199.         //获取起始IP范围#region 获取起始IP范围  
  200.         private long GetStartIp(long recNO)  
  201.         {  
  202.             long offSet = firstStartIp+recNO*7;  
  203.             //objfs.Seek(offSet,SeekOrigin.Begin); 
  204.             objfs.Position=offSet;  
  205.             byte [] buff =new Byte[7];  
  206.             objfs.Read(buff,0,7);  
  207.  
  208.             endIpOff=Convert.ToInt64(buff[4].ToString())+Convert.ToInt64(buff[5].ToString())*256+Convert.ToInt64(buff[6].ToString())*256*256;  
  209.             startIp=Convert.ToInt64(buff[0].ToString())+Convert.ToInt64(buff[1].ToString())*256+Convert.ToInt64(buff[2].ToString())*256*256+Convert.ToInt64(buff[3].ToString())*256*256*256;  
  210.             return startIp;  
  211.         }  
  212.        // 获取结束IP#region 获取结束IP  
  213.         private long GetEndIp()  
  214.         {  
  215.             //objfs.Seek(endIpOff,SeekOrigin.Begin); 
  216.             objfs.Position=endIpOff;  
  217.             byte [] buff =new Byte[5];  
  218.             objfs.Read(buff,0,5);  
  219.             this.endIp=Convert.ToInt64(buff[0].ToString())+Convert.ToInt64(buff[1].ToString())*256+Convert.ToInt64(buff[2].ToString())*256*256+Convert.ToInt64(buff[3].ToString())*256*256*256;  
  220.             this.countryFlag=buff[4];  
  221.             return this.endIp;  
  222.         }  
  223.        //获取国家/区域偏移量#region 获取国家/区域偏移量 
  224.         private string GetCountry()  
  225.         {  
  226.             switch(this.countryFlag)  
  227.             {  
  228.                 case 1:  
  229.                 case 2:  
  230.                     this.country=GetFlagStr(this.endIpOff+4);  
  231.                     this.local=( 1 ==this.countryFlag )?" ":this.GetFlagStr(this.endIpOff+8);  
  232.                     break;  
  233.                 default:  
  234.                     this.country=this.GetFlagStr(this.endIpOff+4);  
  235.                     this.local=this.GetFlagStr(objfs.Position);  
  236.                     break;  
  237.             }  
  238.             return " ";  
  239.         }  
  240.       //获取国家/区域字符串#region 获取国家/区域字符串  
  241.         private string GetFlagStr(long offSet)  
  242.         {  
  243.             int flag=0;  
  244.             byte [] buff = new Byte[3];  
  245.             while(1==1)  
  246.             {  
  247.                 //objfs.Seek(offSet,SeekOrigin.Begin); 
  248.                 objfs.Position=offSet;  
  249.                 flag = objfs.ReadByte();  
  250.                 if(flag==1||flag==2)  
  251.                 {  
  252.                     objfs.Read(buff,0,3);  
  253.                     if(flag==2)  
  254.                     {  
  255.                         this.countryFlag=2;  
  256.                         this.endIpOff=offSet-4;  
  257.                     }  
  258.                     offSet=Convert.ToInt64(buff[0].ToString())+Convert.ToInt64(buff[1].ToString())*256+Convert.ToInt64(buff[2].ToString())*256*256;  
  259.                 }  
  260.                 else  
  261.                 {  
  262.                     break;  
  263.                 }  
  264.             }  
  265.             if(offSet<12)  
  266.                 return " ";  
  267.             objfs.Position=offSet;  
  268.             return GetStr();  
  269.         }  
  270.        //GetStr#region GetStr  
  271.         private string GetStr()  
  272.         {  
  273.             byte lowC=0;  
  274.             byte upC=0;  
  275.             string str="";  
  276.             byte[] buff = newbyte[2];  
  277.             while(1==1)  
  278.             {  
  279.                 lowC= (Byte)objfs.ReadByte();  
  280.                 if(lowC==0)  
  281.                     break;  
  282.                 if(lowC>127)  
  283.                 {  
  284.                     upC=(byte)objfs.ReadByte();  
  285.                     buff[0]=lowC;  
  286.                     buff[1]=upC;  
  287.                     System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312");  
  288.                     str+=enc.GetString(buff);  
  289.                 }  
  290.                 else  
  291.                 {  
  292.                     str+=(char)lowC;  
  293.                 }  
  294.             }  
  295.             return str;  
  296.         }       
  297.       //获取IP地址#region 获取IP地址  
  298.         public string IPLocation()  
  299.         {  
  300.             this.QQwry();  
  301.             return this.country+this.local;  
  302.         }  
  303.         public string IPLocation(string dataPath,string ip)  
  304.         {  
  305.             this.dataPath=dataPath;  
  306.             this.ip=ip;  
  307.             this.QQwry();  
  308.             return this.country+this.local;  
  309.         }  
  310.      
  311.     }  
  312. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值