如何用java获得子网掩码?

yongtaowei 2007-02-06 07:34:44
大家好!

如何用java获得子网掩码?包括linux和windows下的,有什么类可用?
最好给出些代码,谢谢!!
...全文
335 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
blh 2007-02-07
  • 打赏
  • 举报
回复
20?? 现在的知识,越来越不值钱了

public static void main(String[] args) {
try {
Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces();
while (eni.hasMoreElements()) {
NetworkInterface ni = eni.nextElement();
List<InterfaceAddress> lia = ni.getInterfaceAddresses();
Iterator<InterfaceAddress> iia = lia.iterator();
while (iia.hasNext()) {
InterfaceAddress ia = iia.next();
InetAddress a = ia.getAddress();
if (!a.isLoopbackAddress()) {
String ha = a.getHostAddress();
System.out.println("address = " + ha);
short ml = (short) (ia.getNetworkPrefixLength() / 8);
String[] as = ha.split("\\.");
String ns = "";
for (int i = 0; i < ml; i++) {
ns += as[i];
if (i < ml - 1) {
ns += ".";
}
}
System.out.println("subnet = " + ns);
}
}

}
} catch (Exception e) {
e.printStackTrace();
}

}
mouseshuai 2007-02-07
  • 打赏
  • 举报
回复
帮顶吧,我也不知道
yongtaowei 2007-02-07
  • 打赏
  • 举报
回复
怎么没有人回呀?难道都不知道?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧