110,571
社区成员
发帖
与我相关
我的任务
分享
//这个方法测试可行.不过要求位运算
//应该是最快最简单的一种了
string ip;
int a = 1 < <24 | 1 < <16 | 1 < <8 | 1; //1.1.1.1
int b = 2 < <24 | 2 < <16 | 2 < <8 | 2; //2.2.2.2
for(;a <= b;a++)
{
ip = (byte)(a>>24) + ".";
ip += (byte)(a>>16) + ".";
ip += (byte)(a>>8) + ".";
ip += (byte)(a);
}
//这个只是样版适当改改应该可以拿去用了
private static StreamWriter sw;
private static void OutputIpAddress(byte a, byte b, byte c, byte d)
{
Output(string.Format("{0}.{1}.{2}.{3}", a, b, c, d));
}
private static void Output(string s)
{
if (sw == null) sw = File.CreateText(@"E:\desktop\1.txt");
sw.WriteLine(s);
}
System.Net.IPAddress ip = IPAddress.Parse("1.1.1.1");
long ipStart = ip.Address;
string ipStr1 = "1.1.1.1";
string ipStr2 = "1.1.2.2";
string[] ipArray1 = ipStr1.Split(new char[] { '.' });
string[] ipArray2 = ipStr2.Split(new char[] { '.' });
long ipStart = 256 * 256 * 256 * long.Parse(ipArray1[0]) + 256 * 256 * long.Parse(ipArray1[1]) + 256 * long.Parse(ipArray1[2]) + long.Parse(ipArray1[3]);
long ipEnd = 256 * 256 * 256 * long.Parse(ipArray2[0]) + 256 * 256 * long.Parse(ipArray2[1]) + 256 * long.Parse(ipArray2[2]) + long.Parse(ipArray2[3]);
List<string> SearchIPArray = new List<string>();//存放你的中间IP
for (long i = ipStart; i <= ipEnd; i++)
{
long tmp = i;
long a = tmp / (256 * 256 * 256);
tmp -= a * (256 * 256 * 256);
long b = tmp / (256 * 256);
tmp -= b * 256 * 256;
long c = tmp / 256;
tmp -= c * 256;
long d = tmp;
SearchIPArray.Add(a.ToString() + "." + b.ToString() + "." + c.ToString() + "." + d.ToString());
}
string ipStr1 = "1.1.1.1";
string ipStr2 = "1.1.1.4";
string[] ipArray1 = ipStr1.Split(new char[] { '.' });
string[] ipArray2 = ipStr2.Split(new char[] { '.' });
long ipStart = 256 * 256 * 256 * long.Parse(ipArray1[0]) + 256 * 256 * long.Parse(ipArray1[1]) + 256 * long.Parse(ipArray1[2]) + long.Parse(ipArray1[3]);
long ipEnd = 256 * 256 * 256 * long.Parse(ipArray2[0]) + 256 * 256 * long.Parse(ipArray2[1]) + 256 * long.Parse(ipArray2[2]) + long.Parse(ipArray2[3]);
List<string> SearchIPArray = new List<string>();//存放你的中间IP
for (long i = ipStart; i <= ipEnd; i++)
{
long tmp = i;
long a = tmp / (256 * 256 * 256);
tmp -= 256 * 256 * 256;
long b = tmp / (256 * 256);
tmp -= 256 * 256;
long c = tmp / 256;
tmp -= 256;
long d = tmp;
SearchIPArray.Add(a.ToString() + "." + b.ToString() + "." + c.ToString() + "." + d.ToString());
}
public struct IP
{
public byte A;
public byte B;
public byte C;
public byte D;
public IP(byte[] ba)
{
A = ba[0];
B = ba[1];
C = ba[2];
D = ba[3];
}
}
public static void IPSection(IPAddress ip1, IPAddress ip2)
{
IP x = new IP(ip1.GetAddressBytes());
IP y = new IP(ip2.GetAddressBytes());
byte secStart = 1; //这里可以改为0
byte secEnd = 254; //这里可以改为255
for (byte a = x.A; a <= y.A; a++)
{
byte bStart = (a == x.A ? x.B : secStart);
byte bEnd = (a == y.A ? y.B : secEnd);
for (byte b = bStart; b <= bEnd; b++)
{
byte cStart = (a == x.A && b == x.B) ? x.C : secStart;
byte cEnd = (a == y.A && b == y.B) ? y.C : secEnd;
for (byte c = cStart; c <= cEnd; c++)
{
byte dStart = (a == x.A && b == x.B && c == x.C) ? x.D : secStart;
byte dEnd = (a == y.A && b == y.B && c == y.C) ? y.D : secEnd;
for (byte d = dStart; d <= dEnd; d++)
{
OutputIpAddress(a, b, c, d);
}
//这个是测试用的,只输出这一网段最后一位的起止位置,不输出全部。
//Output("========输出网段========");
//OutputIpAddress(a, b, c, dStart);
//OutputIpAddress(a, b, c, dEnd);
}
}
}
}
public string OutPutIp()
{
string ip;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
{
ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else
{
ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
}
reture ip;
}