读取纯真数据库的方法

根据LumaQQ改写而成.

  1
None.gif using  System;
  2 None.gif using  System.IO;
  3 None.gif using  System.Text;
  4 None.gif
  5 None.gif namespace  BDQQ.Data
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// QQWry 的摘要说明。
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public class QQWry
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        第一种模式#region 第一种模式
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 14InBlock.gif        /// 第一种模式
 15ExpandedSubBlockEnd.gif        /// </summary>

 16ExpandedSubBlockEnd.gif        #endregion

 17InBlock.gif        private const byte REDIRECT_MODE_1        = 0x01;
 18InBlock.gif
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        第二种模式#region 第二种模式
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 21InBlock.gif        /// 第二种模式
 22ExpandedSubBlockEnd.gif        /// </summary>

 23ExpandedSubBlockEnd.gif        #endregion

 24InBlock.gif        private const byte REDIRECT_MODE_2        = 0x02;
 25InBlock.gif
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        每条记录长度#region 每条记录长度
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 28InBlock.gif        /// 每条记录长度
 29ExpandedSubBlockEnd.gif        /// </summary>

 30ExpandedSubBlockEnd.gif        #endregion
        
 31InBlock.gif        private const int IP_RECORD_LENGTH        = 7;
 32InBlock.gif
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        数据库文件#region 数据库文件
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 35InBlock.gif        /// 文件对象
 36ExpandedSubBlockEnd.gif        /// </summary>

 37ExpandedSubBlockEnd.gif        #endregion
        
 38InBlock.gif        private FileStream ipFile;
 39InBlock.gif
 40InBlock.gif        private const string unCountry = "未知国家";
 41InBlock.gif        private const string unArea    = "未知地区";
 42InBlock.gif
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        索引开始位置#region 索引开始位置
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 45InBlock.gif        /// 索引开始位置
 46ExpandedSubBlockEnd.gif        /// </summary>

 47ExpandedSubBlockEnd.gif        #endregion

 48InBlock.gif        private long ipBegin;
 49InBlock.gif
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        索引结束位置#region 索引结束位置
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 52InBlock.gif        /// 索引结束位置
 53ExpandedSubBlockEnd.gif        /// </summary>

 54ExpandedSubBlockEnd.gif        #endregion

 55InBlock.gif        private long ipEnd;
 56InBlock.gif
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        IP地址对象#region IP地址对象
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 59InBlock.gif        /// IP对象
 60ExpandedSubBlockEnd.gif        /// </summary>

 61ExpandedSubBlockEnd.gif        #endregion

 62InBlock.gif        private IPLocation loc;
 63InBlock.gif
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        存储文本内容#region 存储文本内容
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 66InBlock.gif        /// 存储文本内容
 67ExpandedSubBlockEnd.gif        /// </summary>

 68ExpandedSubBlockEnd.gif        #endregion

 69InBlock.gif        private byte[] buf;
 70InBlock.gif
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        存储3字节#region 存储3字节
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 73InBlock.gif        /// 存储3字节
 74ExpandedSubBlockEnd.gif        /// </summary>

 75ExpandedSubBlockEnd.gif        #endregion

 76InBlock.gif        private byte[] b3;
 77InBlock.gif
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        存储4字节#region 存储4字节
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 80InBlock.gif        /// 存储4字节IP地址
 81ExpandedSubBlockEnd.gif        /// </summary>

 82ExpandedSubBlockEnd.gif        #endregion

 83InBlock.gif        private byte[] b4;
 84InBlock.gif
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        构造函数#region 构造函数
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 87InBlock.gif        /// 构造函数
 88InBlock.gif        /// </summary>
 89ExpandedSubBlockEnd.gif        /// <param name="ipfile">IP数据库文件绝对路径</param>

 90ExpandedSubBlockEnd.gif        #endregion

 91InBlock.gif        public QQWry( string ipfile )
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
 93InBlock.gif            buf = new byte[100];
 94InBlock.gif            b3 = new byte[3];
 95InBlock.gif            b4 = new byte[4];            
 96InBlock.gif            try
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 98InBlock.gif                ipFile = new FileStream( ipfile,FileMode.Open );
 99ExpandedSubBlockEnd.gif            }

100InBlock.gif            catch( Exception ex )
101ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
102InBlock.gif                throw new Exception( ex.Message );
103ExpandedSubBlockEnd.gif            }
            
104InBlock.gif            ipBegin = readLong4(0);
105InBlock.gif            ipEnd = readLong4(4);
106InBlock.gif            loc = new IPLocation();
107ExpandedSubBlockEnd.gif        }

108InBlock.gif
109InBlock.gif
110ExpandedSubBlockStart.gifContractedSubBlock.gif        根据IP地址搜索#region 根据IP地址搜索
111ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
112InBlock.gif        /// 搜索IP地址搜索
113InBlock.gif        /// </summary>
114InBlock.gif        /// <param name="ip"></param>
115ExpandedSubBlockEnd.gif        /// <returns></returns>

116ExpandedSubBlockEnd.gif        #endregion

117InBlock.gif        public IPLocation SearchIPLocation( string ip )
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            //将字符IP转换为字节
120InBlock.gif            string[] ipSp = ip.Split('.');
121InBlock.gif            if( ipSp.Length != 4 )
122ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
123InBlock.gif                throw new ArgumentOutOfRangeException( "不是合法的IP地址!" );
124ExpandedSubBlockEnd.gif            }

125InBlock.gif            byte[] IP = new byte[4];
126InBlock.gif            forint i = 0; i < IP.Length ; i++ )
127ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
128InBlock.gif                IP[i] = (byte)(Int32.Parse( ipSp[i] ) & 0xFF) ;
129ExpandedSubBlockEnd.gif            }

130InBlock.gif
131InBlock.gif            IPLocation local = null;
132InBlock.gif            long offset = locateIP( IP );
133InBlock.gif
134InBlock.gif            if( offset != -1 )
135ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
136InBlock.gif                local = getIPLocation( offset );
137ExpandedSubBlockEnd.gif            }

138InBlock.gif
139InBlock.gif            if( local == null )
140ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
141InBlock.gif                local = new IPLocation();
142InBlock.gif                local.area = unArea;
143InBlock.gif                local.country = unCountry;
144ExpandedSubBlockEnd.gif            }

145InBlock.gif            return local;
146ExpandedSubBlockEnd.gif        }

147InBlock.gif
148InBlock.gif
149ExpandedSubBlockStart.gifContractedSubBlock.gif        取得具体信息#region 取得具体信息
150ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
151InBlock.gif        /// 取得具体信息
152InBlock.gif        /// </summary>
153InBlock.gif        /// <param name="offset"></param>
154ExpandedSubBlockEnd.gif        /// <returns></returns>

155ExpandedSubBlockEnd.gif        #endregion

156InBlock.gif        private IPLocation getIPLocation( long offset )
157ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
158InBlock.gif            ipFile.Position = offset + 4;
159InBlock.gif            //读取第一个字节判断是否是标志字节
160InBlock.gif            byte one = (byte)ipFile.ReadByte();
161InBlock.gif            if( one == REDIRECT_MODE_1 )
162ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
163InBlock.gif                //第一种模式
164InBlock.gif                //读取国家偏移
165InBlock.gif                long countryOffset = readLong3();
166InBlock.gif                //转至偏移处
167InBlock.gif                ipFile.Position = countryOffset;
168InBlock.gif                //再次检查标志字节
169InBlock.gif                byte b = (byte)ipFile.ReadByte();
170InBlock.gif                if( b == REDIRECT_MODE_2 )
171ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
172InBlock.gif                    loc.country = readString( readLong3() );
173InBlock.gif                    ipFile.Position = countryOffset + 4;
174ExpandedSubBlockEnd.gif                }

175InBlock.gif                else
176InBlock.gif                    loc.country = readString( countryOffset );
177InBlock.gif
178InBlock.gif                //读取地区标志
179InBlock.gif                loc.area = readArea( ipFile.Position );
180InBlock.gif
181ExpandedSubBlockEnd.gif            }

182InBlock.gif            else if( one == REDIRECT_MODE_2 )
183ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
184InBlock.gif                //第二种模式
185InBlock.gif                loc.country = readString( readLong3() );
186InBlock.gif                loc.area = readArea( offset + 8 );
187ExpandedSubBlockEnd.gif            }

188InBlock.gif            else
189ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
190InBlock.gif                //普通模式
191InBlock.gif                loc.country = readString( --ipFile.Position );
192InBlock.gif                loc.area = readString( ipFile.Position );
193ExpandedSubBlockEnd.gif            }

194InBlock.gif            return loc;
195ExpandedSubBlockEnd.gif        }

196InBlock.gif
197InBlock.gif
198ExpandedSubBlockStart.gifContractedSubBlock.gif        取得地区信息#region 取得地区信息
199ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
200InBlock.gif        /// 读取地区名称
201InBlock.gif        /// </summary>
202InBlock.gif        /// <param name="offset"></param>
203ExpandedSubBlockEnd.gif        /// <returns></returns>

204ExpandedSubBlockEnd.gif        #endregion

205InBlock.gif        private string readArea( long offset )
206ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
207InBlock.gif            ipFile.Position = offset;
208InBlock.gif            byte one = (byte)ipFile.ReadByte();
209InBlock.gif            if( one == REDIRECT_MODE_1 || one == REDIRECT_MODE_2 )
210ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
211InBlock.gif                long areaOffset = readLong3( offset + 1 );
212InBlock.gif                if( areaOffset == 0 )
213InBlock.gif                    return unArea;
214InBlock.gif                else
215ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
216InBlock.gif                    return readString( areaOffset );
217ExpandedSubBlockEnd.gif                }

218ExpandedSubBlockEnd.gif            }

219InBlock.gif            else
220ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
221InBlock.gif                return readString( offset );
222ExpandedSubBlockEnd.gif            }

223ExpandedSubBlockEnd.gif        }

224InBlock.gif
225InBlock.gif
226ExpandedSubBlockStart.gifContractedSubBlock.gif        读取字符串#region 读取字符串
227ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
228InBlock.gif        /// 读取字符串
229InBlock.gif        /// </summary>
230InBlock.gif        /// <param name="offset"></param>
231ExpandedSubBlockEnd.gif        /// <returns></returns>

232ExpandedSubBlockEnd.gif        #endregion

233InBlock.gif        private string readString( long offset )
234ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
235InBlock.gif            ipFile.Position = offset;
236InBlock.gif            int i = 0;
237InBlock.gif            for(i = 0, buf[i]=(byte)ipFile.ReadByte();buf[i] != (byte)(0);buf[++i]=(byte)ipFile.ReadByte());
238InBlock.gif            
239InBlock.gif            if( i > 0 )
240InBlock.gif                return Encoding.Default.GetString( buf,0,i );
241InBlock.gif            else
242InBlock.gif                return "";
243ExpandedSubBlockEnd.gif        }

244InBlock.gif
245InBlock.gif
246ExpandedSubBlockStart.gifContractedSubBlock.gif        查找IP地址所在的绝对偏移量#region 查找IP地址所在的绝对偏移量
247ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
248InBlock.gif        /// 查找IP地址所在的绝对偏移量
249InBlock.gif        /// </summary>
250InBlock.gif        /// <param name="ip"></param>
251ExpandedSubBlockEnd.gif        /// <returns></returns>

252ExpandedSubBlockEnd.gif        #endregion

253InBlock.gif        private long locateIP( byte[] ip )
254ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
255InBlock.gif            long m = 0;
256InBlock.gif            int r;
257InBlock.gif
258InBlock.gif            //比较第一个IP项
259InBlock.gif            readIP( ipBegin, b4 );
260InBlock.gif            r = compareIP( ip,b4);
261InBlock.gif            if( r == 0 )
262InBlock.gif                return ipBegin;
263InBlock.gif            else if( r < 0 )
264InBlock.gif                return -1;
265InBlock.gif            //开始二分搜索
266InBlock.gif            forlong i = ipBegin,j=ipEnd; i<j; )
267ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
268InBlock.gif                m = this.getMiddleOffset( i,j );
269InBlock.gif                readIP( m,b4 );
270InBlock.gif                r = compareIP( ip,b4 );
271InBlock.gif                if( r > 0 )
272InBlock.gif                    i = m;
273InBlock.gif                else if( r < 0 )
274ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
275InBlock.gif                    if( m == j )
276ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
277InBlock.gif                        j -= IP_RECORD_LENGTH;
278InBlock.gif                        m = j;
279ExpandedSubBlockEnd.gif                    }

280InBlock.gif                    else
281ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
282InBlock.gif                        j = m;
283ExpandedSubBlockEnd.gif                    }

284ExpandedSubBlockEnd.gif                }

285InBlock.gif                else
286InBlock.gif                    return readLong3( m+4 );
287ExpandedSubBlockEnd.gif            }

288InBlock.gif            m = readLong3( m+4 );
289InBlock.gif            readIP( m,b4 );
290InBlock.gif            r = compareIP( ip,b4 );
291InBlock.gif            if( r <= 0 )
292InBlock.gif                return m;
293InBlock.gif            else
294InBlock.gif                return -1;
295ExpandedSubBlockEnd.gif        }

296InBlock.gif
297InBlock.gif
298ExpandedSubBlockStart.gifContractedSubBlock.gif        读出4字节的IP地址#region 读出4字节的IP地址
299ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
300InBlock.gif        /// 从当前位置读取四字节,此四字节是IP地址
301InBlock.gif        /// </summary>
302InBlock.gif        /// <param name="offset"></param>
303ExpandedSubBlockEnd.gif        /// <param name="ip"></param>

304ExpandedSubBlockEnd.gif        #endregion

305InBlock.gif        private void readIP( long offset, byte[] ip )
306ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
307InBlock.gif            ipFile.Position = offset;
308InBlock.gif            ipFile.Read( ip,0,ip.Length );
309InBlock.gif            byte tmp = ip[0];
310InBlock.gif            ip[0= ip[3];
311InBlock.gif            ip[3= tmp;
312InBlock.gif            tmp = ip[1];
313InBlock.gif            ip[1= ip[2];
314InBlock.gif            ip[2= tmp;
315ExpandedSubBlockEnd.gif        }

316InBlock.gif
317InBlock.gif
318ExpandedSubBlockStart.gifContractedSubBlock.gif        比较IP地址是否相同#region 比较IP地址是否相同
319ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
320InBlock.gif        /// 比较IP地址是否相同
321InBlock.gif        /// </summary>
322InBlock.gif        /// <param name="ip"></param>
323InBlock.gif        /// <param name="beginIP"></param>
324ExpandedSubBlockEnd.gif        /// <returns>0:相等,1:ip大于beginIP,-1:小于</returns>

325ExpandedSubBlockEnd.gif        #endregion

326InBlock.gif        private int compareIP( byte[] ip, byte[] beginIP )
327ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
328InBlock.gif            forint i = 0; i < 4; i++ )
329ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
330InBlock.gif                int r = compareByte( ip[i],beginIP[i] );
331InBlock.gif                if( r != 0 )
332InBlock.gif                    return r;
333ExpandedSubBlockEnd.gif            }

334InBlock.gif            return 0;
335ExpandedSubBlockEnd.gif        }

336InBlock.gif
337InBlock.gif
338ExpandedSubBlockStart.gifContractedSubBlock.gif        比较两个字节是否相等#region 比较两个字节是否相等
339ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
340InBlock.gif        /// 比较两个字节是否相等
341InBlock.gif        /// </summary>
342InBlock.gif        /// <param name="bsrc"></param>
343InBlock.gif        /// <param name="bdst"></param>
344ExpandedSubBlockEnd.gif        /// <returns></returns>

345ExpandedSubBlockEnd.gif        #endregion

346InBlock.gif        private int compareByte( byte bsrc, byte bdst )
347ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
348InBlock.gif            if( ( bsrc&0xFF ) > ( bdst&0xFF ) )
349InBlock.gif                return 1;
350InBlock.gif            else if( (bsrc ^ bdst) == 0 )
351InBlock.gif                return 0;
352InBlock.gif            else
353InBlock.gif                return -1;
354ExpandedSubBlockEnd.gif        }

355InBlock.gif
356InBlock.gif
357ExpandedSubBlockStart.gifContractedSubBlock.gif        根据当前位置读取4字节#region 根据当前位置读取4字节
358ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
359InBlock.gif        /// 从当前位置读取4字节,转换为长整型
360InBlock.gif        /// </summary>
361InBlock.gif        /// <param name="offset"></param>
362ExpandedSubBlockEnd.gif        /// <returns></returns>

363ExpandedSubBlockEnd.gif        #endregion

364InBlock.gif        private long readLong4( long offset )
365ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
366InBlock.gif            long ret = 0;
367InBlock.gif            ipFile.Position = offset;
368InBlock.gif            ret |= ( ipFile.ReadByte() & 0xFF );
369InBlock.gif            ret |= ( ( ipFile.ReadByte() << 8 ) & 0xFF00 );
370InBlock.gif            ret |= ( ( ipFile.ReadByte() << 16 ) & 0xFF0000 );
371InBlock.gif            ret |= ( ( ipFile.ReadByte() << 24 ) & 0xFF000000 );
372InBlock.gif            return ret;
373ExpandedSubBlockEnd.gif        }

374InBlock.gif
375InBlock.gif
376ExpandedSubBlockStart.gifContractedSubBlock.gif        根据当前位置,读取3字节#region 根据当前位置,读取3字节
377ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
378InBlock.gif        /// 根据当前位置,读取3字节
379InBlock.gif        /// </summary>
380InBlock.gif        /// <param name="offset"></param>
381ExpandedSubBlockEnd.gif        /// <returns></returns>

382ExpandedSubBlockEnd.gif        #endregion

383InBlock.gif        private long readLong3( long offset )
384ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
385InBlock.gif            long ret = 0;
386InBlock.gif            ipFile.Position = offset;
387InBlock.gif            ret |= ( ipFile.ReadByte() & 0xFF );
388InBlock.gif            ret |= ( (ipFile.ReadByte() << 8 ) & 0xFF00 );
389InBlock.gif            ret |= ( (ipFile.ReadByte() << 16 ) & 0xFF0000 );
390InBlock.gif            return ret;
391ExpandedSubBlockEnd.gif        }

392InBlock.gif
393InBlock.gif
394ExpandedSubBlockStart.gifContractedSubBlock.gif        从当前位置读取3字节#region 从当前位置读取3字节
395ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
396InBlock.gif        /// 从当前位置读取3字节
397InBlock.gif        /// </summary>
398ExpandedSubBlockEnd.gif        /// <returns></returns>

399ExpandedSubBlockEnd.gif        #endregion

400InBlock.gif        private long readLong3()
401ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
402InBlock.gif            long ret = 0;            
403InBlock.gif            ret |= ( ipFile.ReadByte() & 0xFF );
404InBlock.gif            ret |= ( (ipFile.ReadByte() << 8 ) & 0xFF00 );
405InBlock.gif            ret |= ( (ipFile.ReadByte() << 16 ) & 0xFF0000 );
406InBlock.gif            return ret;
407ExpandedSubBlockEnd.gif        }

408InBlock.gif
409InBlock.gif
410ExpandedSubBlockStart.gifContractedSubBlock.gif        取得begin和end之间的偏移量#region 取得begin和end之间的偏移量
411ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
412InBlock.gif        /// 取得begin和end中间的偏移
413InBlock.gif        /// </summary>
414InBlock.gif        /// <param name="begin"></param>
415InBlock.gif        /// <param name="end"></param>
416ExpandedSubBlockEnd.gif        /// <returns></returns>

417ExpandedSubBlockEnd.gif        #endregion

418InBlock.gif        private long getMiddleOffset( long begin, long end )
419ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
420InBlock.gif            long records = ( end - begin ) / IP_RECORD_LENGTH;
421InBlock.gif            records >>= 1;
422InBlock.gif            if( records == 0 )
423InBlock.gif                records = 1;
424InBlock.gif            return begin + records * IP_RECORD_LENGTH;
425ExpandedSubBlockEnd.gif        }

426ExpandedSubBlockEnd.gif    }
    //class QQWry
427InBlock.gif
428InBlock.gif    public class IPLocation 
429ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
430InBlock.gif        public String country;
431InBlock.gif        public String area;
432InBlock.gif        
433InBlock.gif        public IPLocation() 
434ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
435InBlock.gif            country = area = "";
436ExpandedSubBlockEnd.gif        }

437InBlock.gif        
438InBlock.gif        public IPLocation getCopy() 
439ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
440InBlock.gif            IPLocation ret = new IPLocation();
441InBlock.gif            ret.country = country;
442InBlock.gif            ret.area = area;
443InBlock.gif            return ret;
444ExpandedSubBlockEnd.gif        }

445ExpandedSubBlockEnd.gif    }

446ExpandedBlockEnd.gif}

447 None.gif

转载于:https://www.cnblogs.com/bluedream/archive/2005/07/08/188405.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本模块代码是针对在 2011 年在 CSDN 论坛个发布的“最新 NET 读取纯真IP数据库代码(C#)”源码,做了一次升级,这次升级不是简单的修补,是本人精心的重写,现在只需要 5 分哦,您值得拥有!该源代码不同于网上的代码,网上代码基本可分为两大类,第一类直接使用文件流,通过移动文件流指针(即更改 Stream.Position 属性值)搜索 IP 地址对应的信息,此类代码问题是:其一移动文件指针效率是比较低的(给 Position 赋值),多线程并发时,会重复打开多个文件效率更加底下;第二类是把文件直接加载内存中,通过这种缓冲,速度是提升了,但并没有为多线程环境优化,多线程并发时(如:Web 中每位访客,都是一根线程),意味会重复的读取文件,重复的创建缓存,浪费内存空间。 该源代码特点是考虑到了多线程应用环境(如:Web 每个会话,都是一根线程),设计了缓存对象 QQWryCache 用于管理缓存,用 QQCacheStream 流读取缓存数据。在多线程应用环境中,假设 10 根线程访问同一个纯真 IP 数据库时,只会开辟 1 份缓存,给多根线程共享,避免了不必要的内存浪费。 注1:本模块代码,保证所有静态方法都是线程安全的,但不保证所有实例方法都是线程安全的。 注2:每根线程访问缓存时,请通过 QQWryCache.GetCache 静态方法获取缓存对象。 注3:多根线程获取到的缓存对象,通常都是同一对象,该对象已经考虑了线程同步,不必担心线程安全问题。 /* >>> 使用完全缓存(缓存整个文件,约 8.8MB),调用方法如下: */ QQWryCache cache = QQWryCache.GetCache("qqwry.dat", true); Stream stream = cache.GetCacheStream(); QQWrySearcher searcher = new QQwryScanner(stream); QQWryLocation location = searcher.Query("IP 地址"); Console.WritleLine("Country = {0}, Location = {1}", location.Country, location.Location); /* 完全缓冲, * 缓存一旦初始化完毕,就会自动关闭文件, * 所以不再依赖于文件,因此可以不用关闭缓冲流, * 下面调用 Close 方法,其实没有实际意义,但也不会引发异常。 */ stream.Close(); /* >>> 使用索引缓存(仅缓存索引部分,约 3MB),调用方法如下: <<>> 直接使用文件流(不使用缓存),调用方法如下: <<>> 遍历 IP 数据库。 <<< */ QQWryCache cache = QQWryCache.GetCache("qqwry.dat", true); Stream stream = cache.GetCacheStream(); QQWrySearcher searcher = new QQWrySearcher(stream); // 用 for 循环遍历 for(int i = 0; i < searcher.Count; i++) { QQWryIpLocation item = searcher[i]; Console.WritleLine("Country = {0}, Location = {1}", location.Country, location.Location); } // 用 foreach 循环遍历 foreach(QQWryIpLocation item in searcher) { QQWryIpLocation item = searcher[i]; Console.WritleLine("Country = {0}, Location = {1}", location.Country, location.Location); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值