怎样计算IP地址段?

shmilyfan 2004-08-11 11:15:59
如果我知道一个IP子网号为195.89.15.0 掩码为24 怎样换算成IP网段?像这样195.89.15.1-195.89.15.254。算法怎样?
...全文
1476 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
throw new Exception("ip地址格式不正确!");
回复
sylmoon 2004-08-20
mark two
回复
sspig01 2004-08-19
ApplicationException类按照接口,自己定义一个就可以了
请问楼主是怎样解决的?能写出来参考一下?
万分感谢!
回复
shmilyfan 2004-08-19
OK.谢大家了。问题已经解决了。
回复
shmilyfan 2004-08-19
怎样定义呢?
回复
chesterwoo 2004-08-19
ApplicationException类按照接口,自己定义一个就可以了。
回复
hl_longman 2004-08-19
mark....
回复
shmilyfan 2004-08-19
谢谢你写的程序。
但我编译你写的IP代码没有通过。错误如下:
cannot resolve symbol
public IP(String ipString) throws NumberFormatException,ApplicatioException{
symbol:clas ApplicationException
location:class IP

cannot resolve symbol
if (ipSegment > 255 || ipSegment < 0) throw new ApplicationException("ip地址格式不正确!");
symbol:clas ApplicationException
location:class IP

cannot resolve symbol
throw new ApplicationException("ip地址格式不正确!");
symbol:clas ApplicationException
location:class IP

是不是程序需要加载什么类吗?clas ApplicationException在哪的?
回复
agen10120216 2004-08-19
mark
回复
RESULT:mask = 195.89.15.0-195.89.15.255
回复
/**
* Created by IntelliJ IDEA.
* User: 黄海晏
* Date: 2004-8-11
* Time: 11:34:12
* To change this template use File | Settings | File Templates.
*/

public class IPMask {
//private InetAddress IP;
private IP ip;
private IP mask;
private IP ip2;

public IPMask(String ipMask)throws ApplicationException {
String temp[] = ipMask.split("/");
if (temp.length == 2) {
ip = new IP(temp[0]);
int bit1length = Integer.parseInt(temp[1]);
if (bit1length < 8 || bit1length > 31) throw new ApplicationException("掩码超过范围!");
mask = new IP(((1 << bit1length) - 1) << (32 - bit1length));

if ((ip.getIp() & ~mask.getIp()) != 0) throw new ApplicationException("地址和掩码不匹配!");
ip2 = new IP(ip.getIp() ^ ~mask.getIp());
} else
throw new ApplicationException("ip地址格式不正确!");
}

/* public IP getIp2() {
return ip2;
}
*/
public IP getIp() {
return ip;
}

public String toString() {
return ip + "-" + ip2;
}
public boolean isInScope(IP inetaddress){
if(ip.compareTo(inetaddress)>=0 || ip2.compareTo(inetaddress)<=0) return false;
else return true;
}
public static void main(String[] args)throws ApplicationException {
IPMask mask=new IPMask("195.89.15.0/24");
System.out.println("mask = " + mask);
}
}
回复

/**
* Created by IntelliJ IDEA.
* User: 黄海晏
* Date: 2004-8-11
* Time: 11:35:08
* To change this template use File | Settings | File Templates.
*/
public class IP {
private long ip;
private String[] segment;

public IP(long ip) {
this.ip = ip;
segment = new String[4];

for (int i = 3; i > -1; i--, ip >>= 8)
segment[i] = String.valueOf(ip - ((ip >> 8) << 8));

}

public IP(String ipString) throws NumberFormatException,ApplicationException {
segment = ipString.split("[.]");
if (segment.length == 4) {
ip = 0;
int ipSegment;
for (int i = 0; i < 4; i++) {
ip <<= 8;
ipSegment = Integer.parseInt(segment[i]);
if (ipSegment > 255 || ipSegment < 0) throw new ApplicationException("ip地址格式不正确!");
ip += ipSegment;
}
} else
throw new ApplicationException("ip地址格式不正确!");
}

public String toString() {
return segment[0] + "." + segment[1] + "." + segment[2] + "." + segment[3];
}

public long getIp() {
return ip;
}

public int compareTo(Object object) {
IP ip2 = (IP) object;
if (ip2.getIp() == ip)
return 0;
else if (ip > ip2.getIp())
return 1;
else
return -1;
}

}

回复
shmilyfan 2004-08-11
没有人知道吗?
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2004-08-11 11:15
社区公告
暂无公告