Here you can find the source of getIp()
public static String getIp()
//package com.java2s; //License from project: Apache License import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class Main { private static volatile String cachedIpAddress; public static String getIp() { if (null != cachedIpAddress) { return cachedIpAddress; }//from w w w . ja va 2 s .c o m Enumeration<NetworkInterface> netInterfaces; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (final SocketException ex) { return "unknown ip"; } String localIpAddress = null; while (netInterfaces.hasMoreElements()) { NetworkInterface netInterface = netInterfaces.nextElement(); Enumeration<InetAddress> ipAddresses = netInterface.getInetAddresses(); while (ipAddresses.hasMoreElements()) { InetAddress ipAddress = ipAddresses.nextElement(); if (isPublicIpAddress(ipAddress)) { String publicIpAddress = ipAddress.getHostAddress(); cachedIpAddress = publicIpAddress; return publicIpAddress; } if (isLocalIpAddress(ipAddress)) { localIpAddress = ipAddress.getHostAddress(); } } } cachedIpAddress = localIpAddress; return localIpAddress; } private static boolean isPublicIpAddress(final InetAddress ipAddress) { return !ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && !isV6IpAddress(ipAddress); } private static boolean isLocalIpAddress(final InetAddress ipAddress) { return ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && !isV6IpAddress(ipAddress); } private static boolean isV6IpAddress(final InetAddress ipAddress) { return ipAddress.getHostAddress().contains(":"); } }