C\#QQQWry.Dat 파일 읽 기 IP 조회 실현
QQWry :http://download.csdn.net/detail/fwj380891124/4385554
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Configuration;
namespace BLL
{
public class IPLocationSearch
{
private static readonly QQWry qq = new QQWry(ConfigurationManager.AppSettings["ip"] + "qqwry.dat");
public static IPLocation GetIPLocation(string ip)
{
return qq.SearchIPLocation(ip);
}
}
/*
:
:
BDQQ.Data.QQWry qq=new BDQQ.Data.QQWry("d:\\QQWry.Dat");
BDQQ.Data.IPLocation ip=qq.SearchIPLocation("127.0.0.1");// IP
Console.WriteLine(ip.country);//
Console.WriteLine(ip.area);//
*/
//
// LumaQQ .
/**/
///<summary>
/// QQWry 。
///</summary>
public class QQWry
{
//
#region
/**/
///<summary>
///
///</summary>
#endregion
private const byte REDIRECT_MODE_1 = 0x01;
//
#region
/**/
///<summary>
///
///</summary>
#endregion
private const byte REDIRECT_MODE_2 = 0x02;
//
#region
/**/
///<summary>
///
///</summary>
#endregion
private const int IP_RECORD_LENGTH = 7;
//
#region
/**/
///<summary>
///
///</summary>
#endregion
private FileStream ipFile;
private const string unCountry = " ";
private const string unArea = " ";
//
#region
/**/
///<summary>
///
///</summary>
#endregion
private long ipBegin;
//
#region
/**/
///<summary>
///
///</summary>
#endregion
private long ipEnd;
//IP
#region IP
/**/
///<summary>
/// IP
///</summary>
#endregion
private IPLocation loc;
//
#region
/**/
///<summary>
///
///</summary>
#endregion
private byte[] buf;
// 3
#region 3
/**/
///<summary>
/// 3
///</summary>
#endregion
private byte[] b3;
// 4
#region 4
/**/
///<summary>
/// 4 IP
///</summary>
#endregion
private byte[] b4;
//
#region
/**/
///<summary>
///
///</summary>
///<param name="ipfile">IP </param>
#endregion
public QQWry(string ipfile)
{
buf = new byte[100];
b3 = new byte[3];
b4 = new byte[4];
try
{
ipFile = new FileStream(ipfile, FileMode.Open);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
ipBegin = readLong4(0);
ipEnd = readLong4(4);
loc = new IPLocation();
}
// IP
#region IP
/**/
///<summary>
/// IP
///</summary>
///<param name="ip"></param>
///<returns></returns>
#endregion
public IPLocation SearchIPLocation(string ip)
{
// IP
string[] ipSp = ip.Split('.');
if (ipSp.Length != 4)
{
throw new ArgumentOutOfRangeException(" IP !");
}
byte[] IP = new byte[4];
for (int i = 0; i < IP.Length; i++)
{
IP[i] = (byte)(Int32.Parse(ipSp[i]) & 0xFF);
}
IPLocation local = null;
long offset = locateIP(IP);
if (offset != -1)
{
local = getIPLocation(offset);
}
if (local == null)
{
local = new IPLocation();
local.area = unArea;
local.country = unCountry;
}
return local;
}
//
#region
/**/
///<summary>
///
///</summary>
///<param name="offset"></param>
///<returns></returns>
#endregion
private IPLocation getIPLocation(long offset)
{
ipFile.Position = offset + 4;
//
byte one = (byte)ipFile.ReadByte();
if (one == REDIRECT_MODE_1)
{
//
//
long countryOffset = readLong3();
//
ipFile.Position = countryOffset;
//
byte b = (byte)ipFile.ReadByte();
if (b == REDIRECT_MODE_2)
{
loc.country = readString(readLong3());
ipFile.Position = countryOffset + 4;
}
else
loc.country = readString(countryOffset);
//
loc.area = readArea(ipFile.Position);
}
else if (one == REDIRECT_MODE_2)
{
//
loc.country = readString(readLong3());
loc.area = readArea(offset + 8);
}
else
{
//
loc.country = readString(--ipFile.Position);
loc.area = readString(ipFile.Position);
}
return loc;
}
//
#region
/**/
///<summary>
///
///</summary>
///<param name="offset"></param>
///<returns></returns>
#endregion
private string readArea(long offset)
{
ipFile.Position = offset;
byte one = (byte)ipFile.ReadByte();
if (one == REDIRECT_MODE_1 || one == REDIRECT_MODE_2)
{
long areaOffset = readLong3(offset + 1);
if (areaOffset == 0)
return unArea;
else
{
return readString(areaOffset);
}
}
else
{
return readString(offset);
}
}
//
#region
/**/
///<summary>
///
///</summary>
///<param name="offset"></param>
///<returns></returns>
#endregion
private string readString(long offset)
{
ipFile.Position = offset;
int i = 0;
for (i = 0, buf[i] = (byte)ipFile.ReadByte(); buf[i] != (byte)(0); buf[++i] = (byte)ipFile.ReadByte()) ;
if (i > 0)
return Encoding.Default.GetString(buf, 0, i);
else
return "";
}
// IP
#region IP
/**/
///<summary>
/// IP
///</summary>
///<param name="ip"></param>
///<returns></returns>
#endregion
private long locateIP(byte[] ip)
{
long m = 0;
int r;
// IP
readIP(ipBegin, b4);
r = compareIP(ip, b4);
if (r == 0)
return ipBegin;
else if (r < 0)
return -1;
//
for (long i = ipBegin, j = ipEnd; i < j; )
{
m = this.getMiddleOffset(i, j);
readIP(m, b4);
r = compareIP(ip, b4);
if (r > 0)
i = m;
else if (r < 0)
{
if (m == j)
{
j -= IP_RECORD_LENGTH;
m = j;
}
else
{
j = m;
}
}
else
return readLong3(m + 4);
}
m = readLong3(m + 4);
readIP(m, b4);
r = compareIP(ip, b4);
if (r <= 0)
return m;
else
return -1;
}
// 4 IP
#region 4 IP
/**/
///<summary>
/// , IP
///</summary>
///<param name="offset"></param>
///<param name="ip"></param>
#endregion
private void readIP(long offset, byte[] ip)
{
ipFile.Position = offset;
ipFile.Read(ip, 0, ip.Length);
byte tmp = ip[0];
ip[0] = ip[3];
ip[3] = tmp;
tmp = ip[1];
ip[1] = ip[2];
ip[2] = tmp;
}
// IP
#region IP
/**/
///<summary>
/// IP
///</summary>
///<param name="ip"></param>
///<param name="beginIP"></param>
///<returns>0: ,1:ip beginIP,-1: </returns>
#endregion
private int compareIP(byte[] ip, byte[] beginIP)
{
for (int i = 0; i < 4; i++)
{
int r = compareByte(ip[i], beginIP[i]);
if (r != 0)
return r;
}
return 0;
}
//
#region
/**/
///<summary>
///
///</summary>
///<param name="bsrc"></param>
///<param name="bdst"></param>
///<returns></returns>
#endregion
private int compareByte(byte bsrc, byte bdst)
{
if ((bsrc & 0xFF) > (bdst & 0xFF))
return 1;
else if ((bsrc ^ bdst) == 0)
return 0;
else
return -1;
}
// 4
#region 4
/**/
///<summary>
/// 4 ,
///</summary>
///<param name="offset"></param>
///<returns></returns>
#endregion
private long readLong4(long offset)
{
long ret = 0;
ipFile.Position = offset;
ret |= (ipFile.ReadByte() & 0xFF);
ret |= ((ipFile.ReadByte() << 8) & 0xFF00);
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000);
ret |= ((ipFile.ReadByte() << 24) & 0xFF000000);
return ret;
}
// , 3
#region , 3
/**/
///<summary>
/// , 3
///</summary>
///<param name="offset"></param>
///<returns></returns>
#endregion
private long readLong3(long offset)
{
long ret = 0;
ipFile.Position = offset;
ret |= (ipFile.ReadByte() & 0xFF);
ret |= ((ipFile.ReadByte() << 8) & 0xFF00);
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000);
return ret;
}
// 3
#region 3
/**/
///<summary>
/// 3
///</summary>
///<returns></returns>
#endregion
private long readLong3()
{
long ret = 0;
ret |= (ipFile.ReadByte() & 0xFF);
ret |= ((ipFile.ReadByte() << 8) & 0xFF00);
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000);
return ret;
}
// begin end
#region begin end
/**/
///<summary>
/// begin end
///</summary>
///<param name="begin"></param>
///<param name="end"></param>
///<returns></returns>
#endregion
private long getMiddleOffset(long begin, long end)
{
long records = (end - begin) / IP_RECORD_LENGTH;
records >>= 1;
if (records == 0)
records = 1;
return begin + records * IP_RECORD_LENGTH;
}
} //class QQWry
public class IPLocation
{
public String country;
public String area;
public IPLocation()
{
country = area = "";
}
public IPLocation getCopy()
{
IPLocation ret = new IPLocation();
ret.country = country;
ret.area = area;
return ret;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception Class에서 에러 코드 해석 ~초기초편~직장에서 C# 프로젝트가 내뿜는 오류 코드를 구문 분석하고 오류의 위치를 확인하기 위해 Exception class를 활용할 수 있었습니다. 지금까지 Exception Class 에 대해서 별로 파악할 수 없었기 때...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.