⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipsearcher.cs

📁 C#写的IP归属地查询的程序
💻 CS
字号:
using System;
using System.IO;
using System.Text;
using System.Data;


namespace SmartIP
{
	/// <summary>
	/// IpSearcher 的摘要说明。
	/// </summary>
	public class IpSearcher
	{
		private Stream dbStream;
		private uint ipCount = 0;
		private uint indexPos = 0;//索引区开始位置
		public IpSearcher()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		private String getStr(Stream stream)
		{
			byte[] byteArray = new byte[256];
			Encoding gb2312 = Encoding.GetEncoding("GB2312");
			int count = 0;
			while(true)
			{
				byteArray[count] = (byte)stream.ReadByte();
				if(0 == byteArray[count])
					break;
				count++;
			}
			char[] charArray = new char[count];
			gb2312.GetChars(byteArray, 0, count, charArray, 0);
			string result = "";
			for(int i=0; i<charArray.Length && '\0'!=(int)charArray[i]; i++)
			{
				result = result + charArray[i];
			}
			return result;
		}

		private uint byteArray2uint(byte[] byteArray)
		{
			uint result = 0;
			for(int i=0; i<byteArray.Length; i++)
			{
				result = (uint)byteArray[i] * (uint)Math.Pow(256, i) + result;
			}
			return result;
		}

		public int loadIpDB(String fileName) //加载IP数据库
		{
			if (!File.Exists(fileName))
			{
				return 0;//文件未找到
			}
			try 
			{
				dbStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
			}
			catch (Exception e)
			{
				Console.WriteLine("Exception in IpSearcher.loadIpDB(): " + e.Message);
				return 1;//无法打开文件
			}
			uint beginInt = 0;
			uint endInt = 0;
			byte[] tmpByte = new byte[4];
			dbStream.Read(tmpByte,0, 4);
			beginInt = byteArray2uint(tmpByte);
			dbStream.Read(tmpByte,0, 4);
			endInt = byteArray2uint(tmpByte);
			uint total = endInt - beginInt;
			if (total%7 != 0) 
			{
				return 2;//文件损坏
			}
			ipCount = total/7;
			ipCount++;
			indexPos = beginInt;
			//dbStream.
			return -1;
		}

		private uint str2IP(String strIP)
		{
			uint iIP = 0;
			uint tmpIP = 0;
			for(int i=0; i<strIP.Length; i++)
			{
				char curChar = strIP[i];
				if('.' == curChar)
				{
					iIP = 256 * iIP + tmpIP;
					tmpIP = 0;
				} 
				else
				{
					tmpIP = 10 * tmpIP + curChar - '0';
				}
			}
			iIP = 256 * iIP + tmpIP;
			return iIP;
		}

		public String ip2Address(String strIP) //61.48.0.0(2) 61.53.27.0  61.131.46.0 3/1026818048/3d.34/61.52
		{

			String result = "";
			if(null == dbStream)
			{
				return "";
			}
			uint iIP = str2IP(strIP);
			uint beginPos = 0;
			uint endPos = ipCount;
			byte[] tmpByte = new byte[4];
			while(true)
			{
				if(beginPos >= endPos - 1)
				{
					break;
				}
				dbStream.Position = indexPos + (beginPos + endPos)/2 * 7;
				dbStream.Read(tmpByte,0, 4);
				uint tmpIP = byteArray2uint(tmpByte);
				if(iIP < tmpIP)
				{
					endPos = (beginPos + endPos)/2;
				} 
				else 
				{
					beginPos = (beginPos + endPos)/2;
				}
			}
			byte[] tmp3Byte = new byte[3];
			dbStream.Position = indexPos + 7 * beginPos + 4;
			dbStream.Read(tmp3Byte,0, 3);
			uint ipPos = byteArray2uint(tmp3Byte);//ipPos要查找的IP纪录的起始位置
			dbStream.Position = ipPos;
			dbStream.Read(tmpByte,0, 4);
			if(iIP <= byteArray2uint(tmpByte))//找到该IP的位置信息
			{ 
				uint curPos = ipPos + 4;
				if(0x01 == dbStream.ReadByte()) 
				{
					dbStream.Read(tmp3Byte,0, 3);
					curPos = byteArray2uint(tmp3Byte);
					dbStream.Position = curPos;
				}
				else
				{
					dbStream.Seek(-1, SeekOrigin.Current);
				}
				if(0x02 == dbStream.ReadByte())
				{
					dbStream.Read(tmp3Byte,0, 3);
					curPos = byteArray2uint(tmp3Byte);
					dbStream.Position = curPos;
				}
				else
				{
					dbStream.Seek(-1, SeekOrigin.Current);
				}
				//找到国家
				result = "国家:" + getStr(dbStream);
				//(0为失败中止标志)
				//地区
				if( 0x02 == dbStream.ReadByte()) //找到国家以后还发现0x02跳过去找地区(国家/地区)
				{
					dbStream.Read(tmp3Byte,0, 3);
					curPos = byteArray2uint(tmp3Byte);
				}
				else
				{
					dbStream.Seek(-1, SeekOrigin.Current);
				}
				result = result + "地区:" + getStr(dbStream);
			}
			else 
			{
				result = "未知数据";
			}
			return result;
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -