您的位置:首页 > 理论基础 > 计算机网络

问题小结(9)-android获取有线网络的ip

2012-06-08 19:25 417 查看
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
if (intf.getName().toLowerCase().equals("eth0") || intf.getName().toLowerCase().equals("wlan0")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ipaddress = inetAddress.getHostAddress().toString();
if(!ipaddress.contains("::")){//ipV6的地址
return ipaddress;
}
}
}
} else {
continue;
}
}


红色地方表示:仅过滤无线和有线的ip. networkInterface是有很多的名称的 比如sim0,remt1.....等等.我不需要用到就直接过滤了

绿色的地方表示: 过滤掉ipv6的地址.不管无线还是有线 都有这个地址, 我这边显示地址大体是:fe80::288:88ff:fe00:1%eth0 fe80::ee17:2fff:fece:c0b4%wlan0 一般都是出现在第一次循环.第二次循环就是真正的ipv4的地址.

获取有线网关的问题还没找到相关资料,待续。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: