asp.net 如何对IP地址进行操作 比如,录入IP操作,在文本框内输入起始IP和结束IP,点击按钮那么所有IP就录入到数据库中。主要疑问有两个:1、文本框如何控制输入。2、如何不用循环将IP写入数据库中,用4个FOR循环? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 MASKTEXTBOX正则验证System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse("");long ip= ipaddress.Address;IP++判断范围 问题1 正则来判断 正则表达式\d+\.\d+\.\d+\.\d+问题2 没想到更好的办法 批量插入。 string pstr = @"^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])$"; string ip1str = "111.111.111.111"; string ip2str = "111.111.112.111"; if (!Regex.IsMatch(ip1str, pstr) || !Regex.IsMatch(ip2str, pstr)) { //错误 } ip1str = Regex.Replace(ip1str, @"\.", m => { n--; return "*" + Math.Pow(255, n).ToString() + "+"; }); ip2str = Regex.Replace(ip2str, @"\.", m => { n--; return "*" + Math.Pow(255, n).ToString() + "+"; }); int n = 4; long ip1 = Convert.ToInt64(new DataTable().Compute(ip1str, "")); n = 4; long ip2 = Convert.ToInt64(new DataTable().Compute(ip2str, "")); for (long i = ip1; i <= ip2; i++) { long num = i; int i1 = (int)(num / (255 * 255 * 255)); num -= i1 * 255 * 255 * 255; long i2 = (int)(num / (255 * 255)); num -= i2 * 255 * 255; long i3 = (int)(num / 255); num -= i3 * 255; long i4 = num; string ip = string.Format("{0}.{1}.{2}.{3}", i1, i2, i3, i4); Response.Write(ip + "<br>"); } System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse(textbox1.Text);long startip= ipaddress.Address;ipaddress = System.Net.IPAddress.Parse(textbox2Text);long endip=ipaddress.Address;while(startip<endip){} 或System.Net.IPAddress x = System.Net.IPAddress.Parse("startIp"); var xl = BitConverter.ToUInt64(x.GetAddressBytes(), 0); //详细改了一编,应该不会报错了 string pstr = @"^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])$"; string ip1str = "192.222.0.1"; string ip2str = "192.224.0.5"; if (!Regex.IsMatch(ip1str, pstr) || !Regex.IsMatch(ip2str, pstr)) { //错误 } string[] ip1s = ip1str.Split('.'); string[] ip2s = ip2str.Split('.'); long ip1 = Convert.ToInt64(ip1s[0]) * 256 * 256 * 256 + Convert.ToInt64(ip1s[1]) * 256 * 256 + Convert.ToInt64(ip1s[2]) * 256 + Convert.ToInt64(ip1s[3]); long ip2 = Convert.ToInt64(ip2s[0]) * 256 * 256 * 256 + Convert.ToInt64(ip2s[1]) * 256 * 256 + Convert.ToInt64(ip2s[2]) * 256 + Convert.ToInt64(ip2s[3]); for (long i = ip1; i <= ip2; i++) { long num = i; long i1 = (int)(num / (256 * 256 * 256)); num -= (long)i1 * 256 * 256 * 256; long i2 = (int)(num / (256 * 256)); num -= (long)i2 * 256 * 256; long i3 = (int)(num / 256); num -= i3 * 256; long i4 = num; //最后一位为0的不要 if (i4 == 0) continue; string ip = string.Format("{0}.{1}.{2}.{3}", i1, i2, i3, i4); Response.Write(ip + "<br>"); } GridView问题,比较简单 数据表 .net点击button弹出按钮对话框 一个控件中图片上传的问题 为什么这么多.net资料都没人来?大家帮我找找原因,散分! 为什么会出现"无法找到列0" Dataset在asp.net中的保持 各位老大师傅请指点!!!小弟关于网页对话框的,在线等!!!! 网页上有错误! NetAdvantage控件问题 在线等,如何获得当前服务器的Windows系统路径 js写的全选,点击分页全选丢失 本地运行好着,传到服务器上出现问题
正则验证
System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse("");
long ip= ipaddress.Address;IP++判断范围
string ip1str = "111.111.111.111";
string ip2str = "111.111.112.111";
if (!Regex.IsMatch(ip1str, pstr) || !Regex.IsMatch(ip2str, pstr))
{
//错误
}
ip1str = Regex.Replace(ip1str, @"\.", m => { n--; return "*" + Math.Pow(255, n).ToString() + "+"; });
ip2str = Regex.Replace(ip2str, @"\.", m => { n--; return "*" + Math.Pow(255, n).ToString() + "+"; }); int n = 4;
long ip1 = Convert.ToInt64(new DataTable().Compute(ip1str, ""));
n = 4;
long ip2 = Convert.ToInt64(new DataTable().Compute(ip2str, "")); for (long i = ip1; i <= ip2; i++)
{
long num = i;
int i1 = (int)(num / (255 * 255 * 255));
num -= i1 * 255 * 255 * 255;
long i2 = (int)(num / (255 * 255));
num -= i2 * 255 * 255;
long i3 = (int)(num / 255);
num -= i3 * 255;
long i4 = num; string ip = string.Format("{0}.{1}.{2}.{3}", i1, i2, i3, i4);
Response.Write(ip + "<br>");
}
long startip= ipaddress.Address;
ipaddress = System.Net.IPAddress.Parse(textbox2Text);
long endip=ipaddress.Address;
while(startip<endip)
{}
或System.Net.IPAddress x = System.Net.IPAddress.Parse("startIp");
var xl = BitConverter.ToUInt64(x.GetAddressBytes(), 0);
string ip1str = "192.222.0.1";
string ip2str = "192.224.0.5";
if (!Regex.IsMatch(ip1str, pstr) || !Regex.IsMatch(ip2str, pstr))
{
//错误
} string[] ip1s = ip1str.Split('.');
string[] ip2s = ip2str.Split('.'); long ip1 = Convert.ToInt64(ip1s[0]) * 256 * 256 * 256 + Convert.ToInt64(ip1s[1]) * 256 * 256 + Convert.ToInt64(ip1s[2]) * 256 + Convert.ToInt64(ip1s[3]);
long ip2 = Convert.ToInt64(ip2s[0]) * 256 * 256 * 256 + Convert.ToInt64(ip2s[1]) * 256 * 256 + Convert.ToInt64(ip2s[2]) * 256 + Convert.ToInt64(ip2s[3]); for (long i = ip1; i <= ip2; i++)
{
long num = i; long i1 = (int)(num / (256 * 256 * 256));
num -= (long)i1 * 256 * 256 * 256; long i2 = (int)(num / (256 * 256));
num -= (long)i2 * 256 * 256; long i3 = (int)(num / 256);
num -= i3 * 256; long i4 = num; //最后一位为0的不要
if (i4 == 0) continue; string ip = string.Format("{0}.{1}.{2}.{3}", i1, i2, i3, i4);
Response.Write(ip + "<br>");
}