用java获得广播地址
2008-02-23 09:28:40来源:互联网 阅读 ()
如何计算网络的广播地址呢,下面就是算法:
假如计算机的IP位址是192.15.156.205,子网掩码是255.255.255.224,
先把子网掩码255.255.255.224做 NOT 运算﹐可以得出﹕
00000000.00000000.00000000.00011111
然后再和IP做一次 OR 运算﹐就可以得到 Broadcast Address:
11000000.00001111.10011100.11001101 OR 00000000.00000000.00000000.00011111
(192.15.156.205 OR 255.255.255.224)
得出﹕ 11000000.00001111.10011100.11011111
(192.15.156.223)
192.15.156.223就是那个子网的广播地址了.
下面就是实现这个算法的Java类清单.
public class BroadcastAddr {
private String netmask = "";
private String netaddr = "";
private String netbroadcastaddr = "";
/**
* @param args
*/
public static void main(String[] args) {
BroadcastAddr info = new BroadcastAddr();
try {
System.out.println(Inet4Address.getByName("ooad-navysea")
.toString());
System.out.println(Inet4Address.getAllByName("localhost").length);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
info.setNetaddr("192.168.1.106");
info.setNetmask("255.255.255.0");
info.execCalc();
System.out.println(info.getNetbroadcastaddr());
}
public String getNetaddr() {
return netaddr;
}
public void setNetaddr(String netaddr) {
this.netaddr = netaddr;
}
public String getNetbroadcastaddr() {
return netbroadcastaddr;
}
public void setNetbroadcastaddr(String netbroadcastaddr) {
this.netbroadcastaddr = netbroadcastaddr;
}
public String getNetmask() {
return netmask;
}
public void setNetmask(String netmask) {
this.netmask = netmask;
}
public void execCalc() {
String[] tm = this.getNetmask().split("\\.");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tm.length; i ) {
tm[i] = String.valueOf(~(Integer.parseInt(tm[i])));
}
String[] tm2 = this.getNetaddr().split("\\.");
for (int i = 0; i < tm.length; i ) {
tm[i] = String.valueOf((Integer.parseInt(tm2[i]))
| (Integer.parseInt(tm[i])));
}
for (int i = 0; i < tm.length; i ) {
sb.append(intTOstr(tm[i]));
sb.append(".");
}
// sb.delete(sb.length()-1,sb.length());
sb.deleteCharAt(sb.length() - 1);
this.netbroadcastaddr = parseIp(sb.toString());
}
private String intTOstr(int num) {
String tm = "";
tm = Integer.toBinaryString(num);
int c = 8 - tm.length();
// 如果二进制数据少于8位,在前面补零.
for (int i = 0; i < c; i ) {
tm = "0" tm;
}
// 1111 1111 1111 1111 1111 1111 1101 1110
// 如果小于零,则只取最后的8位.
if (c < 0)
tm = tm.substring(24, 32);
return tm;
}
private String intTOstr(String num) {
return intTOstr(Integer.parseInt(num));
}
private String parseIp(String fbg) {
String[] tm = fbg.split("\\.");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tm.length; i ) {
sb.append(strToint(tm[i]));
sb.append(".");
}
// sb.delete(sb.length()-1,1);
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* 把二进制数转换为十进制.
*
* @param str
* @return
*/
private int strToint(String str) {
int total = 0;
int top = str.length();
for (int i = 0; i < str.length(); i ) {
// System.out.println(str.charAt(i) str.substring(i,i 1));
String h = String.valueOf(str.charAt(i));
// System.out.println(h ":" top ":" total);
top--;
total = ((int) Math.pow(2, top)) * (Integer.parseInt(h));
}
return total;
}
}
上一篇: J2EE下开发Ajax程序开发-DWR的介绍
下一篇: JFCUnit测试GUI的一个实例(配置篇)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash