www.pudn.com > network-java.rar > HostAccount.java
package netManager.discovery; /** *Title: HostAccount
*Description: 估算子网内可能存在的最多主机
*Copyright: Copyright (c) 2006
*Company:
* @author:谢飞 * @version 1.0 */ import java.net.InetAddress; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import netManager.framework.exception.BaseException; public class HostAccount{ private String subnetMask; private Log log = LogFactory.getLog("HostAccount"); public HostAccount() throws BaseException{ subnetMask = SubnetMask.getSubnetMask(); } /** *根据子网掩码求得当前子网内最多可能主机数 */ public int getMaxHosts(){ int maxHosts = 1; try{ String binaryIP = Convert.toBinary(subnetMask); //将IP地址从十进制转换为二进制形式 int position = binaryIP.indexOf("0"); String calPart = binaryIP.substring(position); for(int i=1;i<=calPart.length();i++){ maxHosts = maxHosts * 2; } double d = (maxHosts/255); int i = new Double(d).intValue(); maxHosts = maxHosts - ( 2 * i); //IP地址中末尾为0或255的为特殊用途,不计在Hosts中 //System.out.println("子网内最多主机数为: " + hosts); } catch(Exception e){ e.printStackTrace(); log.error(e); } return maxHosts; } }