日期:2014-05-18  浏览次数:20811 次

请问这个题目怎么做???感谢!
假如我们有一开始IP地址:,有一结束IP地址,做一个程序能打印出其间所有的IP地址,比如开始IP:192.168.0.1,结束IP:192.168.1.3,
则应输出:192.168.0.1,192.168.0.2,192.168.0.3...192.168.1.3
要求:开始IP地址,结束IP地址作为程序参数或定义为常量

这个题目如何做啊,帮帮忙,谢谢了!

------解决方案--------------------
字符串分割,然后写一个循环输出

ip的4段的每个最大值为255
------解决方案--------------------
IPv4 是32位的, 转成二进制+1 就是下一个IP

InetAddress ip = InetAddress.getByName("192.168.0.1");
ip.getAddress()[3] = (byte) (ip.getAddress()[3] + 1);
System.out.println(ip);
------解决方案--------------------
InetAddress ip = InetAddress.getByName("192.168.0.1");
byte[] b = ip.getAddress();
b[3] = (byte) (b[3] + 1);
ip = InetAddress.getByAddress(b);
System.out.println(ip);
------解决方案--------------------
InetAddress ip = InetAddress.getByName("192.168.0.1"); 
byte[] b = ip.getAddress(); 
b[3] = (byte) (b[3] + 1); 
ip = InetAddress.getByAddress(b); 
System.out.println(ip);
可以打印出来