www.pudn.com > network-java.rar > PingIP.java


package netManager.discovery; 
 
/** 
 * 

Title: PingIP

*

Description: 返回ping目标的IP地址(该子网内可能存在的所有主机IP地址)集合

*

Copyright: Copyright (c) 2006

*

Company:

* @author:谢飞 * @version 1.0 */ import java.util.Vector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import netManager.framework.exception.BaseException; public class PingIP{ int hostAccount; String subnetAddress; Vector IPAddress; private Log log = LogFactory.getLog("PingIP"); public PingIP() throws BaseException{ hostAccount = DiscoveryUtil.getMaxHosts(); //得到估算主机数 subnetAddress = DiscoveryUtil.getSubnetAddress(); //得到子网地址 IPAddress = new Vector(); } /** *求子网中所有主机IP地址方法如下:(针对C类网) *1.如果主机数小于254,直接在子网IP地址的第四段(P4)分别累加至254,如:子网IP为192.168.0.0,掩码为:255.255.255.0 * ,算得主机数为:254,则该子网内的主机IP为:192.168.0.1-----192.168.0.254 *2.如果主机数大于254,每当P4累加至254时,子网地址的第三部分(P3)加1,并将P4置零,再重新累加P4. * 如:子网IP为192.168.128.0,掩码为:255.255.128.0,则该子网内的主机IP地址为:192.168.128.1---192.168.255.254 */ public Vector getPingIP(){ try{ int [] subAddPart = Convert.getPartIP(subnetAddress); String strIP; double tempD = hostAccount/254; //计算是否需要加子网地址的P3部分,tempD>1需要,tempD=1不需要 System.out.print(tempD); int tempI = new Double(tempD).intValue(); for(int i=1; i<=tempD; i++){ if(i>1){ //i>1表示需要外循环,即主机数超过254,P4部分不够加 subAddPart[2] = subAddPart[2] + 1; //子网地址P4加到254后,P3加1,并将P4置零 } subAddPart[3] = 0; for(int j=1; j<=254; j++){ subAddPart[3] = subAddPart[3] + 1; //在子网地址P4依次加1 strIP = (new Integer(subAddPart[0])).toString() + "." + (new Integer(subAddPart[1])).toString() + "." + (new Integer(subAddPart[2])).toString() + "." + (new Integer(subAddPart[3])).toString(); IPAddress.addElement(strIP); //log.debug(strIP); } } } catch(Exception e){ e.printStackTrace(); log.error(e); } return IPAddress; } }