jdk1.5获取子网广播地址

用jdk1.6可以很容易的获取子网广播地址,但是1.5并没有实现相应的方法。我写了如下代码,已经在window xp和linux redhat中测试通过。
第一个类:实现子网广播地址
[code]import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Vector;

public class SubnetAddress{
private static Process p;

public void SubnetAddress(){
}

/**
* 获取本机IP
* @return
*/
private String getIpAddressFromLinux(){
Enumeration netInterfaces;
String result=null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while(netInterfaces.hasMoreElements()){
NetworkInterface ni=(NetworkInterface)netInterfaces.nextElement();
ip=(InetAddress) ni.getInetAddresses().nextElement();
if( !ip.isLoopbackAddress()&& ip.getHostAddress().indexOf(":")==-1)
{
result=ip.getHostAddress();
break;
}
else
ip=null;
}
} catch (SocketException e) {
e.printStackTrace();
}
return result;
}

/**
* 获取本机IP
* @return
*/
private String getIpAddress(){
String result=null;
InetAddress inet;
try {
inet = InetAddress.getLocalHost();
result = inet.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return result;
}

/**
* 使用ipconfig/all命令
* 获取windows系统上的子网掩码
* @return
*/
private String getSubnetAddressForWindow(){
String cmd = "cmd.exe /c ipconfig/all"; //window下获取子网掩码的命令
String find ="Subnet Mask";//子网掩码的提示

Vector ipConfigs;
ipConfigs = execute(cmd);
Object[] ipConfigArray=ipConfigs.toArray();
String result = null;
for(int i=0;i<ipConfigArray.length;i++){
String ipConfig = ipConfigArray[i].toString();
if(!ipConfig.equalsIgnoreCase("") && ipConfig.indexOf(find)!=-1){
String[] subnet = ipConfig.split(":");
result = subnet[1].substring(1);
break;
}
}
return result;
}

/**
* 使用ifconfig命令
* 获取linux系统上的子网掩码
* @return
*/
private String getSubnetAddressForLinux(){
String cmd = "ifconfig"; //linux下获取子网掩码的命令
String find = "Mask";//子网掩码的提示

Vector ipConfigs;
ipConfigs = execute(cmd);
Object[] ipConfigArray=ipConfigs.toArray();
String result = null;
for(int i=0;i<ipConfigArray.length;i++){
String ipConfig = ipConfigArray[i].toString();
if(!ipConfig.equalsIgnoreCase("") && ipConfig.indexOf(find)!=-1){

String[] subnet1 = ipConfig.split(find);
String[] subnet = subnet1[1].split(":");
result = subnet[1];
break;
}
}
return result;
}

/**
* 执行命令
* @param shellCommand
* @return
*/
private Vector execute(String shellCommand){
try{
Start(shellCommand);
Vector vResult=new Vector();
DataInputStream in=new DataInputStream(p.getInputStream());
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
String line;
do{
line=reader.readLine();
if(line==null){
break;
}
else{
vResult.addElement(line);
}
}while(true);
reader.close();
return vResult;
}catch(Exception e){
return null;
}
}

private void Start(String shellCommand){
try{
if(p!=null){
p.destroy();
p=null;
}
Runtime sys=Runtime.getRuntime();
p=sys.exec(shellCommand);
}catch(Exception e){
e.printStackTrace();
}
}

/**
* 根据本机IP和子网掩码,计算子网广播地址
* @param ip 本机ip
* @param subnet 本机子网掩码
* @return
*/
public String calculate() {
String os = System.getProperty("os.name").substring(0, 3);
String subnet;
if(os.equalsIgnoreCase("win")){
subnet = getSubnetAddressForWindow();
}
else{
subnet = getSubnetAddressForLinux();
}
String ip = getIpAddress();

String[] ips = ip.split("\\.");
String[] subnets = subnet.split("\\.");
StringBuffer sb = new StringBuffer();
for(int i = 0; i < ips.length; i++) {
ips[i] = String.valueOf((~Integer.parseInt(subnets[i]))|(Integer.parseInt(ips[i])));
sb.append(turnToStr(Integer.parseInt(ips[i])));
if(i != (ips.length-1))
sb.append(".");
}
return turnToIp(sb.toString());
}

/**
* 把带符号整形转换为二进制
* @param num
* @return
*/
private String turnToStr(int num) {
String str = "";
str = Integer.toBinaryString(num);
int len = 8 - str.length();
// 如果二进制数据少于8位,在前面补零.
for (int i = 0; i < len; i++) {
str = "0" + str;
}
//如果num为负数,转为二进制的结果有32位,如1111 1111 1111 1111 1111 1111 1101 1110
//则只取最后的8位.
if (len < 0)
str = str.substring(24, 32);
return str;
}

/**
* 把二进制形式的ip,转换为十进制形式的ip
* @param str
* @return
*/
private String turnToIp(String str){
String[] ips = str.split("\\.");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ips.length; i++) {
sb.append(turnToInt(ips[i]));
sb.append(".");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}

/**
* 把二进制转换为十进制
* @param str
* @return
*/
private int turnToInt(String str){
int total = 0;
int top = str.length();
for (int i = 0; i < str.length(); i++) {
String h = String.valueOf(str.charAt(i));
top--;
total += ((int) Math.pow(2, top)) * (Integer.parseInt(h));
}
return total;
}
}[/code]

第二个类:调用上个类中的方法
[code]import java.net.SocketException;
import java.net.UnknownHostException;

public class test{
public static void main(String[] args) throws SocketException, UnknownHostException{
System.out.println(System.getProperty("os.name"));
SubnetAddress sa = new SubnetAddress();
String s = sa.calculate();
System.out.println(s);
}
}[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值