1.須要 QQWry.Dat IP 地址數據庫
數據庫
2輔助類庫網絡
1 1 using System; 2 2 using System.Collections.Generic; 3 3 using System.IO; 4 4 using System.Linq; 5 5 using System.Text; 6 6 using System.Text.RegularExpressions; 7 7 using System.Threading.Tasks; 8 8 9 9 namespace Utilities 10 10 { 11 11 /// <summary> 12 12 /// 根據IP地址查詢所在地 13 13 /// </summary> 14 14 public class IPScanerHelper 15 15 { 16 16 #region 私有成員 17 17 private string dataPath; 18 18 private string ip; 19 19 private string country; 20 20 private string local; 21 21 22 22 private long firstStartIp = 0; 23 23 private long lastStartIp = 0; 24 24 private FileStream objfs = null; 25 25 private long startIp = 0; 26 26 private long endIp = 0; 27 27 private int countryFlag = 0; 28 28 private long endIpOff = 0; 29 29 private string errMsg = null; 30 30 #endregion 31 31 32 32 #region 構造函數 33 33 public IPScanerHelper() 34 34 { 35 35 // 36 36 // TODO: 在此處添加構造函數邏輯 37 37 // 38 38 } 39 39 #endregion 40 40 41 41 #region 公共屬性 42 42 public string DataPath 43 43 { 44 44 set { dataPath = value; } 45 45 } 46 46 public string IP 47 47 { 48 48 set { ip = value; } 49 49 } 50 50 public string Country 51 51 { 52 52 get { return country; } 53 53 } 54 54 public string Local 55 55 { 56 56 get { return local; } 57 57 } 58 58 public string ErrMsg 59 59 { 60 60 get { return errMsg; } 61 61 } 62 62 #endregion 63 63 64 64 #region 搜索匹配數據 65 65 private int QQwry() 66 66 { 67 67 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]))"; 68 68 Regex objRe = new Regex(pattern); 69 69 Match objMa = objRe.Match(ip); 70 70 if (!objMa.Success) 71 71 { 72 72 this.errMsg = "IP格式錯誤"; 73 73 return 4; 74 74 } 75 75 76 76 long ip_Int = this.IpToInt(ip); 77 77 int nRet = 0; 78 78 if (ip_Int >= IpToInt("127.0.0.0") && ip_Int <= IpToInt("127.255.255.255")) 79 79 { 80 80 this.country = "本機內部環回地址"; 81 81 this.local = ""; 82 82 nRet = 1; 83 83 } 84 84 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"))) 85 85 { 86 86 this.country = "網絡保留地址"; 87 87 this.local = ""; 88 88 nRet = 1; 89 89 } 90 90 objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read); 91 91 try 92 92 { 93 93 //objfs.Seek(0,SeekOrigin.Begin); 94 94 objfs.Position = 0; 95 95 byte[] buff = new Byte[8]; 96 96 objfs.Read(buff, 0, 8); 97 97 firstStartIp = buff[0] + buff[1] * 256 + buff[2] * 256 * 256 + buff[3] * 256 * 256 * 256; 98 98 lastStartIp = buff[4] * 1 + buff[5] * 256 + buff[6] * 256 * 256 + buff[7] * 256 * 256 * 256; 99 99 long recordCount = Convert.ToInt64((lastStartIp - firstStartIp) / 7.0); 100 100 if (recordCount <= 1) 101 101 { 102 102 country = "FileDataError"; 103 103 objfs.Close(); 104 104 return 2; 105 105 } 106 106 long rangE = recordCount; 107 107 long rangB = 0; 108 108 long recNO = 0; 109 109 while (rangB < rangE - 1) 110 110 { 111 111 recNO = (rangE + rangB) / 2; 112 112 this.GetStartIp(recNO); 113 113 if (ip_Int == this.startIp) 114 114 { 115 115 rangB = recNO; 116 116 break; 117 117 } 118 118 if (ip_Int > this.startIp) 119 119 rangB = recNO; 120 120 else 121 121 rangE = recNO; 122 122 } 123 123 this.GetStartIp(rangB); 124 124 this.GetEndIp(); 125 125 if (this.startIp <= ip_Int && this.endIp >= ip_Int) 126 126 { 127 127 this.GetCountry(); 128 128 this.local = this.local.Replace("(咱們必定要解放臺灣!!!)", ""); 129 129 } 130 130 else 131 131 { 132 132 nRet = 3; 133 133 this.country = "未知"; 134 134 this.local = ""; 135 135 } 136 136 objfs.Close(); 137 137 return nRet; 138 138 } 139 139 catch 140 140 { 141 141 return 1; 142 142 } 143 143 144 144 } 145 145 #endregion 146 146 147 147 #region IP地址轉換成Int數據 148 148 private long IpToInt(string ip) 149 149 { 150 150 char[] dot = new char[] { '.' }; 151 151 string[] ipArr = ip.Split(dot); 152 152 if (ipArr.Length == 3) 153 153 ip = ip + ".0"; 154 154 ipArr = ip.Split(dot); 155 155 156 156 long ip_Int = 0; 157 157 long p1 = long.Parse(ipArr[0]) * 256 * 256 * 256; 158 158 long p2 = long.Parse(ipArr[1]) * 256 * 256; 159 159 long p3 = long.Parse(ipArr[2]) * 256; 160 160 long p4 = long.Parse(ipArr[3]); 161 161 ip_Int = p1 + p2 + p3 + p4; 162 162 return ip_Int; 163 163 } 164 164 #endregion 165 165 166 166 #region int轉換成IP 167 167 private string IntToIP(long ip_Int) 168 168 { 169 169 long seg1 = (ip_Int & 0xff000000) >> 24; 170 170 if (seg1 < 0) 171 171 seg1 += 0x100; 172 172 long seg2 = (ip_Int & 0x00ff0000) >> 16; 173 173 if (seg2 < 0) 174 174 seg2 += 0x100; 175 175 long seg3 = (ip_Int & 0x0000ff00) >> 8; 176 176 if (seg3 < 0) 177 177 seg3 += 0x100; 178 178 long seg4 = (ip_Int & 0x000000ff); 179 179 if (seg4 < 0) 180 180 seg4 += 0x100; 181 181 string ip = seg1.ToString() + "." + seg2.ToString() + "." + seg3.ToString() + "." + seg4.ToString(); 182 182 183 183 return ip; 184 184 } 185 185 #endregion 186 186 187 187 #region 獲取起始IP範圍 188 188 private long GetStartIp(long recNO) 189 189 { 190 190 long offSet = firstStartIp + recNO * 7; 191 191 //objfs.Seek(offSet,SeekOrigin.Begin); 192 192 objfs.Position = offSet; 193 193 byte[] buff = new Byte[7]; 194 194 objfs.Read(buff, 0, 7); 195 195 196 196 endIpOff = Convert.ToInt64(buff[4].ToString()) + Convert.ToInt64(buff[5].ToString()) * 256 + Convert.ToInt64(buff[6].ToString()) * 256 * 256; 197 197 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; 198 198 return startIp; 199 199 } 200 200 #endregion 201 201 202 202 #region 獲取結束IP 203 203 private long GetEndIp() 204 204 { 205 205 //objfs.Seek(endIpOff,SeekOrigin.Begin); 206 206 objfs.Position = endIpOff; 207 207 byte[] buff = new Byte[5]; 208 208 objfs.Read(buff, 0, 5); 209 209 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; 210 210 this.countryFlag = buff[4]; 211 211 return this.endIp; 212 212 } 213 213 #endregion 214 214 215 215 #region 獲取國家/區域偏移量 216 216 private string GetCountry() 217 217 { 218 218 switch (this.countryFlag) 219 219 { 220 220 case 1: 221 221 case 2: 222 222 this.country = GetFlagStr(this.endIpOff + 4); 223 223 this.local = (1 == this.countryFlag) ? " " : this.GetFlagStr(this.endIpOff + 8); 224 224 break; 225 225 default: 226 226 this.country = this.GetFlagStr(this.endIpOff + 4); 227 227 this.local = this.GetFlagStr(objfs.Position); 228 228 break; 229 229 } 230 230 return " "; 231 231 } 232 232 #endregion 233 233 234 234 #region 獲取國家/區域字符串 235 235 private string GetFlagStr(long offSet) 236 236 { 237 237 int flag = 0; 238 238 byte[] buff = new Byte[3]; 239 239 while (1 == 1) 240 240 { 241 241 //objfs.Seek(offSet,SeekOrigin.Begin); 242 242 objfs.Position = offSet; 243 243 flag = objfs.ReadByte(); 244 244 if (flag == 1 || flag == 2) 245 245 { 246 246 objfs.Read(buff, 0, 3); 247 247 if (flag == 2) 248 248 { 249 249 this.countryFlag = 2; 250 250 this.endIpOff = offSet - 4; 251 251 } 252 252 offSet = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256 + Convert.ToInt64(buff[2].ToString()) * 256 * 256; 253 253 } 254 254 else 255 255 { 256 256 break; 257 257 } 258 258 } 259 259 if (offSet < 12) 260 260 return " "; 261 261 objfs.Position = offSet; 262 262 return GetStr(); 263 263 } 264 264 #endregion 265 265 266 266 #region GetStr 267 267 private string GetStr() 268 268 { 269 269 byte lowC = 0; 270 270 byte upC = 0; 271 271 string str = ""; 272 272 byte[] buff = new byte[2]; 273 273 while (1 == 1) 274 274 { 275 275 lowC = (Byte)objfs.ReadByte(); 276 276 if (lowC == 0) 277 277 break; 278 278 if (lowC > 127) 279 279 { 280 280 upC = (byte)objfs.ReadByte(); 281 281 buff[0] = lowC; 282 282 buff[1] = upC; 283 283 System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312"); 284 284 str += enc.GetString(buff); 285 285 } 286 286 else 287 287 { 288 288 str += (char)lowC; 289 289 } 290 290 } 291 291 return str; 292 292 } 293 293 #endregion 294 294 295 295 #region 獲取IP地址 296 296 public string IPLocation() 297 297 { 298 298 this.QQwry(); 299 299 return this.country + this.local; 300 300 } 301 301 public string IPLocation(string dataPath, string ip) 302 302 { 303 303 this.dataPath = dataPath; 304 304 this.ip = ip; 305 305 this.QQwry(); 306 306 return this.country + this.local; 307 307 } 308 308 #endregion 309 309 } 310 310 }
3.測試ide
1 string IPAddress = NetHelper.GetIPAddress(); 2 IPScanerHelper objScan = new IPScanerHelper(); 3 objScan.IP = IPAddress; 4 objScan.DataPath = Server.MapPath("~/Resource/IPScaner/QQWry.Dat"); 5 string IPAddressName =objScan.IPLocation();