怎样计算IP地址段?

shmilyfan 2004-08-11 11:15:59
如果我知道一个IP子网号为195.89.15.0 掩码为24 怎样换算成IP网段?像这样195.89.15.1-195.89.15.254。算法怎样?
...全文
1523 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
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
  • 打赏
  • 举报
回复
没有人知道吗?

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧