java 获取本机ip及mac地址
2020-11-22 18:31
标签:class java ext get string int com .net name res amp package com.achun.test; import java.net.Inet4Address; public class HelloWorld { public static void main(String[] args) { } private static String getHostAddress(boolean ipv6, boolean
connectTest) if ((ni.isUp()) && (!ni.isLoopback()) &&
(!ni.isVirtual())) { private static InetAddress getInetAddress(NetworkInterface ni, boolean
ipv6) public static boolean isReachable(String ip) } java 获取本机ip及mac地址,搜素材,soscw.com java 获取本机ip及mac地址 标签:class java ext get string int com .net name res amp 原文地址:http://www.cnblogs.com/charlie-badegg/p/3700881.html
import java.net.Inet6Address;
import
java.net.InetAddress;
import java.net.NetworkInterface;
import
java.util.Enumeration;
// TODO Auto-generated method
stub
System.out.println("HelloWorld");
String ip =
getHostAddress(false,true);
System.out.println(ip);
{
Enumeration netInterfaces = null;
InetAddress address = null;
try {
netInterfaces =
NetworkInterface.getNetworkInterfaces();
while
(netInterfaces.hasMoreElements())
{
NetworkInterface ni =
(NetworkInterface)netInterfaces.nextElement();
address = getInetAddress(ni, ipv6);
if (address != null)
if (connectTest) {
if
(address.isReachable(3000))
return
address.getHostAddress();
}
else return
address.getHostAddress();
}
}
}
catch
(Exception e) {
}
return "";
}
{
Enumeration enumeration = ni.getInetAddresses();
InetAddress address = null;
while (enumeration.hasMoreElements()) {
address = (InetAddress)enumeration.nextElement();
if ((!ipv6)
&& ((address instanceof Inet4Address)))
return address;
if ((ipv6) && ((address instanceof Inet6Address)))
return address;
}
return null;
}
{
try
{
return InetAddress.getByName(ip).isReachable(3000); } catch
(Exception e) {
}
return false;
}
上一篇:JS 字符unicode转换函数
下一篇:C语言中函数返回数组