标签:
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); } } } }
========================
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()); }标签:
原文地址:http://www.cnblogs.com/zcm123/p/4272833.html