NetUtils { public static InetAddress getLocalAddress() { InetAddress localAddress = null; try { localAddress = InetAddress.getLocalHost(); if (isValidAddress(localAddress)) { return localAddress; } } catch (Throwable e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); } } try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); if (interfaces != null) { while (interfaces.hasMoreElements()) { try { NetworkInterface network = interfaces.nextElement(); Enumeration<InetAddress> addresses = network.getInetAddresses(); while (addresses.hasMoreElements()) { try { InetAddress address = addresses.nextElement(); if (isValidAddress(address)) { return address; } } catch (Throwable e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); } } } } catch (Throwable e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); } } } } } catch (Throwable e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); } } if (LOGGER.isErrorEnabled()) { LOGGER.error(LogCodes.getLog(LogCodes.ERROR_GET_HOST_FAIL)); } return localAddress; }  static boolean isInvalidPort(int port); static boolean isRandomPort(int port); static int getAvailablePort(String host, int port); static int getAvailablePort(String host, int port, int maxPort); static boolean isLocalHost(String host); static boolean isAnyHost(String host); static boolean isIPv4Host(String host); static boolean isHostInNetworkCard(String host); static String getLocalIpv4(); static InetAddress getLocalAddress(); static String toAddressString(InetSocketAddress address); static String toIpString(InetSocketAddress address); static String getLocalHostByRegistry(String registryIp); static List<InetSocketAddress> getIpListByRegistry(String registryIp); static boolean isMatchIPByPattern(String whiteList, String localIP); static String connectToString(InetSocketAddress local, InetSocketAddress remote); static String channelToString(SocketAddress local1, SocketAddress remote1); static boolean canTelnet(String ip, int port, int timeout); static void joinMulticastGroup(MulticastSocket multicastSocket, InetAddress multicastAddress); static void setInterface(MulticastSocket multicastSocket, boolean preferIpv6); static final String ANYHOST; static final Pattern IPV4_PATTERN; }
@Test public void getLocalAddress() throws Exception { }