码迷,mamicode.com
首页 > 系统相关 > 详细

获取本机IP(适用于Linux系统)

时间:2017-08-07 15:14:53      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:eth   socket   获取本机ip   equals   ace   elements   soc   ext   bre   

获取本机IP(适用于Linux系统)
/**
* @desc 获取本机IP(适用于Linux系统)
* @return Ip
*/
public static String getLocalIP() {
String ip = "";
try {
Enumeration<?> e1 = (Enumeration<?>) NetworkInterface
.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e1.nextElement();
if (!ni.getName().equals("eth0")) {
continue;
} else {
Enumeration<?> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = (InetAddress) e2.nextElement();
if (ia instanceof Inet6Address)
continue;
ip = ia.getHostAddress();
}
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}
 
原创

获取本机IP(适用于Linux系统)

标签:eth   socket   获取本机ip   equals   ace   elements   soc   ext   bre   

原文地址:http://www.cnblogs.com/ameizai/p/7298990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!