ip地址归属地实现 我想做一个ip地址归属地查询的页面,我有那个ip地址段所属地区的txt,麻烦各位大虾给我实现的代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只能教你方法,最好别是拿来主义,再说,某位说过受人于鱼不如授人于渔.你根据要查找的IP,和你那个txr或者数据库匹配,然后在输出不就行了? 关键是我不知道怎么来读取txt文本内容.用stream读取出来的不知道怎么跟实际ip地址对比! look http://lumaqq.linuxsir.org/article/qqwry_format_detail.html 哈哈.能不能把那个IP的TXT传给我.我做一下,我给你源码. 呵呵.做出来了.不过没有全面测试.未必正确vista+vs2008+.net framework3.5IP类代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Data.OleDb;using System.Data;namespace IP归属地查询{ class IP { private string strIP = null; public string IPString { get { return strIP; } } private IP() { } public IP(string strIP) { if (Check(strIP)) { this.strIP = strIP; } else { throw (new IPException("不正确的IP格式!")); } } private bool Check(string strIP) { if (Regex.IsMatch(strIP, @"^(?!192\.168\.)((([1-9]?[0-9])|1[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}(([1-9]?[0-9])|1[0-9]{2}|2([0-4][0-9]|5[0-5]))$")) { return true; } return false; } public string GetAddress() { double dblIP=CacuIP(); OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bendibaoip.mdb"); OleDbDataAdapter ada = new OleDbDataAdapter("SELECT country,city FROM IP_SoftYes WHERE ip1<=" + dblIP + " AND ip2>=" + dblIP, conn); DataSet dst=new DataSet(); ada.Fill(dst); if (dst.Tables[0].Rows.Count > 0) { return dst.Tables[0].Rows[0][0].ToString() + dst.Tables[0].Rows[0][1].ToString(); } return "对不起!数据库中无此IP信息!"; } private double CacuIP() { double dblIP = 0; string[] arrIPs = this.IPString.Split('.'); for (int i = 0; i <= 3; i++) { dblIP += Convert.ToDouble(arrIPs[i]) * System.Math.Pow(256, (3 - i)); } return dblIP - 1; } } /// <summary> /// IP异常 /// </summary> public class IPException : Exception { private string strMessage; public override string Message { get { return strMessage; } } public IPException(string strMessage) { this.strMessage = strMessage; } } } 窗体代码:private void button1_Click(object sender, EventArgs e) { try { IP ip = new IP(this.textBox1.Text.Trim()); this.label1.Text = ip.GetAddress(); } catch (IPException ipe) { MessageBox.Show(ipe.Message); } } 我这个IP数据库是网上下的mdb文件.格式如下:IP1 ip2 country city67829760 67895295 美国 夏威夷............我是根据作者提供的ASP当中的JAVASCRIPTE算法转换过来的.不一定正确.因为对JAVASCRIPT一无所知. 哥哥 你这个方法我也会做,但是我想要的是从txt文件中读取并且操作数据,而不是在数据库中读取操作数据 泛型如何判断 {B}元素 在 {A}元素 里面的个数 从Excel获取数据 将数据插入数据库的问题~~ 怪!datagridview中双击后的出现的窗体点左上角的关闭时关闭条在datagridview中没有消失 多窗体 数据 绑定的问题 c# Convert.ToString 与 .ToString() 有什么区别? xml 怎么把">" 转成表达式中的 > ? TrackingLayer的问题!!!在线等,谢谢大家!!! 想把得到的流保存成htm文件,报错:此流不支持查找操作。 关于Timer控件 谢谢 Font构造函数问题???
你根据要查找的IP,和你那个txr或者数据库匹配,然后在输出不就行了?
vista+vs2008+.net framework3.5IP类代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.Data;namespace IP归属地查询
{
class IP
{
private string strIP = null;
public string IPString
{
get
{
return strIP;
}
} private IP()
{ } public IP(string strIP)
{
if (Check(strIP))
{
this.strIP = strIP;
}
else
{
throw (new IPException("不正确的IP格式!"));
}
} private bool Check(string strIP)
{
if (Regex.IsMatch(strIP, @"^(?!192\.168\.)((([1-9]?[0-9])|1[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}(([1-9]?[0-9])|1[0-9]{2}|2([0-4][0-9]|5[0-5]))$"))
{
return true;
}
return false;
}
public string GetAddress()
{
double dblIP=CacuIP();
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bendibaoip.mdb");
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT country,city FROM IP_SoftYes WHERE ip1<=" + dblIP + " AND ip2>=" + dblIP, conn);
DataSet dst=new DataSet();
ada.Fill(dst);
if (dst.Tables[0].Rows.Count > 0)
{
return dst.Tables[0].Rows[0][0].ToString() + dst.Tables[0].Rows[0][1].ToString();
}
return "对不起!数据库中无此IP信息!";
} private double CacuIP()
{
double dblIP = 0;
string[] arrIPs = this.IPString.Split('.');
for (int i = 0; i <= 3; i++)
{ dblIP += Convert.ToDouble(arrIPs[i]) * System.Math.Pow(256, (3 - i));
}
return dblIP - 1;
}
}
/// <summary>
/// IP异常
/// </summary>
public class IPException : Exception
{
private string strMessage;
public override string Message
{
get
{
return strMessage;
}
}
public IPException(string strMessage)
{
this.strMessage = strMessage;
} }
}
{
try
{
IP ip = new IP(this.textBox1.Text.Trim());
this.label1.Text = ip.GetAddress();
}
catch (IPException ipe)
{
MessageBox.Show(ipe.Message); }
}
IP1 ip2 country city67829760 67895295 美国 夏威夷............
我是根据作者提供的ASP当中的JAVASCRIPTE算法转换过来的.不一定正确.因为对JAVASCRIPT一无所知.