读书人

透过起始IP和IP总个数求得所有的连续

发布时间: 2013-09-28 10:01:20 作者: rapoo

通过起始IP和IP总个数,求得所有的连续IP
如题.举例起始IP为192.168.2.2,IP总个数为3,那么要求得的所有IP的为192.168.2.2,192.168.2.3,192.168.2.4。再举个例子,起始IP为192.168.2.253,IP总个数为5那么要求得的所有IP为192.168.2.253,192.168.2.254,192.168.3.1,192.168.3.2,192.168.3.3。我想大家应该明白是什么意思了。求大家帮助啊。谢谢各位了!急急急!100分送上。 获取IP
[解决办法]
这个老早以前写过,很简单的,就是字符串按.分割,取最后一项然后根据输入的数,累加即可。到了255的时候是一个拐点,取最后一项的前一项+1,最后一项初始值为1.
[解决办法]
请补充一下基础知识“ipv4 和256进制的关系,ip2long,long2ip的转换规则,System.Net.IPAddress类的使用说明”

相信你看完这些东西,自己写不需要3分钟就ok。祝:编程愉快
[解决办法]
refer:

static void Main(string[] args)
{
string ip = "254.254.253.253";//这个为测试ip
int count = 259;//这个为输入的个数
string[] ss = ip.Split('.');

int num1 = Convert.ToInt32(ss[0]);
int num2 = Convert.ToInt32(ss[1]);
int num3 = Convert.ToInt32(ss[2]);
int num4 = Convert.ToInt32(ss[3]);

for (int i = 0; i < count; i++)
{
num4++;
if (num4 > 254)
{
num3++;
num4 = 1;
}

if (num3 > 254)
{
num2++;
num3 = 1;
num4 = 1;
}

if (num2 > 254)
{
num1++;
num2 = 1;
num3 = 1;
num4 = 1;
}

Console.WriteLine(num1 + "." + num2 + "." + num3 + "." + num4);
}
}

[解决办法]
这家伙也太懒了。
[解决办法]
额,还是这么着把,免的你被带沟里

var ipstart = System.Net.IPAddress.Parse("192.168.2.253").Address;
//Address被否决,不过可以用,其实有另外的标准解法,不过是给你演示这关系就无所谓了
//否决就否决,如果想知道标准解是什么,稍微研究一下就ok
var res= Enumerable.Range(0, 5).Select(p => new System.Net.IPAddress(ipstart + p).ToString());


ps:这里也并不是演示System.Net.IPAddress怎么使用,只是告诉你ip和long有相互转换规则,部分工作微软已经做了,如果有兴趣当然也可以自己做,而且自己实现其实也不复杂256进制几步位移就过来了,同样几步位移又可以反回去
------解决方案--------------------


192.168.2.254,192.168.3.1

这可不是什么“连续ip”啊。

不但要跳过广播ip,而且你要考虑人家对于网段的管理方式。可不是随便从一个网段调到另外一个网段就叫做“连续”的。
[解决办法]

引用:
refer:
static void Main(string[] args)
{
string ip = "254.254.253.253";//这个为测试ip
int count = 259;//这个为输入的个数
string[] ss = ip.Split('.');

int num1 = Convert.ToInt32(ss[0]);
int num2 = Convert.ToInt32(ss[1]);
int num3 = Convert.ToInt32(ss[2]);
int num4 = Convert.ToInt32(ss[3]);

for (int i = 0; i < count; i++)
{
num4++;
if (num4 > 254)
{
num3++;
num4 = 1;
}

if (num3 > 254)
{
num2++;
num3 = 1;
num4 = 1;
}

if (num2 > 254)
{
num1++;
num2 = 1;
num3 = 1;
num4 = 1;
}

Console.WriteLine(num1 + "." + num2 + "." + num3 + "." + num4);
}
}

基本思路是这个
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static int IpToNumber(string Ip)
{
return BitConverter.ToInt32(Ip.Split('.').Select(x => Convert.ToByte(x)).Reverse().ToArray(), 0);
}

static string NumberToIp(int Ip)
{
return string.Join(".", BitConverter.GetBytes(Ip).Select(x => x.ToString()).Reverse().ToArray());
}

static void Main(string[] args)
{
string IP = "192.188.0.2";
int n = 5;
for (int i = IpToNumber(IP); i < IpToNumber(IP) + n; i++)


Console.WriteLine(NumberToIp(i));
}
}
}


192.188.0.2
192.188.0.3
192.188.0.4
192.188.0.5
192.188.0.6
Press any key to continue . . .
[解决办法]

// 字符型ip地址转成无符号整数
Func<string, uint> ip2uint = ip => BitConverter.ToUInt32(IPAddress.Parse(ip).GetAddressBytes().Reverse().ToArray(), 0);
// 无符号整数转回ip地址
Func<uint, string> uint2ip = i => new IPAddress(BitConverter.GetBytes(i).Reverse().ToArray()).ToString();

uint ip0 = ip2uint("192.168.2.253"); // 起始ip地址
int count = 5; // 取多少个ip地址
string[] ips = Enumerable.Range(0, int.MaxValue) // 从0开始无限循环
.Where(i => ((ip0 + i + 1) & 0xff) > 1) // 如果最低位是0或255(+1<=1),则跳过
.Select(i => uint2ip((uint) (ip0 + i))) // 转换成字符型ip地址
.Take(count) // 取前count个
.ToArray(); // 转成字符串数组

读书人网 >C#

热点推荐