package getlocalip;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;
public class GetLocalIpUtil {
private static Set machineIps = Collections.synchronizedSet(new HashSet());
public static Set get() {
if (!machineIps.isEmpty()) {
return machineIps;
}
try {
InetAddress[] addresses = getAllLocalUsingNetworkInterface();
if (addresses == null || addresses.length == 0) {
System.out.println("定时器本机可用的ip地址失败,getAllLocalUsingNetworkInterface返回0个地址");
return machineIps;
}
for (InetAddress address : addresses) {
if (!address.getHostAddress().toString().equals("127.0.0.1")) {
machineIps.add(address.getHostAddress().toString());
}
}
System.out.println("定时器本机可用的ip地址成功,ip地址:" + machineIps.toString());
} catch (UnknownHostException e) {
System.out.println("获取定时器物理IP失败!message={}" + e.getMessage());
}
return machineIps;
}
private static InetAddress[] getAllLocalUsingNetworkInterface() throws UnknownHostException {
List