如何使用Java获取本地系统的Subnet
掩码地址?
答案 0 :(得分:25)
localhost接口的第一个地址的网络掩码:
InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
更完整的方法:
InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
System.out.println(address.getNetworkPrefixLength());
}
/ 24表示255.255.255。
答案 1 :(得分:5)
答案 2 :(得分:4)
我发现:
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
要获取ipv6的子网掩码,我们可以使用:
networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
要获取ipv4的子网掩码,我们可以使用:
networkInterface.getInterfaceAddresses().get(1).getNetworkPrefixLength();
答案 3 :(得分:3)
我设计了一个非常简单的IPv4解决方案。我需要在这里为子网生成网络掩码,以便正确地委派这些子网。我知道我可以生成一个包含32个可能掩码的表,但我更喜欢每次计算它。
所以这是我的解决方案。
/*
* Get network mask for the IP address and network prefix specified...
* The network mask will be returned has an IP, thus you can
* print it out with .getHostAddress()...
*/
public static InetAddress getIPv4LocalNetMask(InetAddress ip, int netPrefix) {
try {
// Since this is for IPv4, it's 32 bits, so set the sign value of
// the int to "negative"...
int shiftby = (1<<31);
// For the number of bits of the prefix -1 (we already set the sign bit)
for (int i=netPrefix-1; i>0; i--) {
// Shift the sign right... Java makes the sign bit sticky on a shift...
// So no need to "set it back up"...
shiftby = (shiftby >> 1);
}
// Transform the resulting value in xxx.xxx.xxx.xxx format, like if
/// it was a standard address...
String maskString = Integer.toString((shiftby >> 24) & 255) + "." + Integer.toString((shiftby >> 16) & 255) + "." + Integer.toString((shiftby >> 8) & 255) + "." + Integer.toString(shiftby & 255);
// Return the address thus created...
return InetAddress.getByName(maskString);
}
catch(Exception e){e.printStackTrace();
}
// Something went wrong here...
return null;
}
您只需使用您要使用的IP和前缀调用它,它将为您生成网络掩码。
答案 4 :(得分:2)
答案 5 :(得分:2)
以下是如何从WIFI连接获取子掩码的答案:link
我根据自己的需要调整了它,现在是:
private static String intToIP(int ipAddress) {
String ret = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
(ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
return ret;
}
public static String GetSubnetMask_WIFI() {
WifiManager wifiManager = (WifiManager) Global.getMainActivity()
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
DhcpInfo dhcp = wifiManager.getDhcpInfo();
String mask = intToIP(dhcp.netmask);
return mask;
}
答案 6 :(得分:2)
总之,获取掩码的方法如下:
public String mascara() throws SocketException{
try{
InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
prefijo =
""+networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
int shft = 0xffffffff<<(32-
networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength());
int oct1 = ((byte) ((shft&0xff000000)>>24)) & 0xff;
int oct2 = ((byte) ((shft&0x00ff0000)>>16)) & 0xff;
int oct3 = ((byte) ((shft&0x0000ff00)>>8)) & 0xff;
int oct4 = ((byte) (shft&0x000000ff)) & 0xff;
mascara = oct1+"."+oct2+"."+oct3+"."+oct4;
// System.out.println(""+mascara);
}catch(UnknownHostException e){
System.out.println("Error: "+e);
}
return mascara;
}
答案 7 :(得分:1)
FWIW,过去我曾尝试使用InterfaceAddress.getNetworkPrefixLength()和InterfaceAddress.getBroadcast(),但它们没有返回准确的信息(这是在Windows上,Sun JDK 1.6.0更新10)。网络前缀长度为128(不是24,它在我的网络上),返回的广播地址是255.255.255.255(不是192.168.1.255,它在我的网络上)。
詹姆斯
更新:我刚刚找到了解决方案:
http://forums.sun.com/thread.jspa?threadID=5277744
您需要阻止Java使用IPv6,以免它通过IPv6进入IPv4。 在命令行中添加-Djava.net.preferIPv4Stack = true可以修复InterfaceAddress.getNetworkPrefixLength()和InterfaceAddress.getBroadcast()的结果。
答案 8 :(得分:1)
您可以将获得的值转换为标准文本格式,如下所示:
short prflen=...getNetworkPrefixLength();
int shft = 0xffffffff<<(32-prflen);
int oct1 = ((byte) ((shft&0xff000000)>>24)) & 0xff;
int oct2 = ((byte) ((shft&0x00ff0000)>>16)) & 0xff;
int oct3 = ((byte) ((shft&0x0000ff00)>>8)) & 0xff;
int oct4 = ((byte) (shft&0x000000ff)) & 0xff;
String submask = oct1+"."+oct2+"."+oct3+"."+oct4;
答案 9 :(得分:0)
String local=InetAddress.getLocalHost().getHostAddress();
String[] ip_component = local.split("\\.");
String subnet=ip_component[0]+"."+ip_component[1]+"."+ip_component[2]+".";
这对我有用。 这里变量子网具有子网地址。