Here you can find the source of getLocalIp()
public static String getLocalIp()
//package com.java2s; //License from project: Open Source License import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class Main { public static String getLocalIp() { try {/*w w w .j a v a2 s . c om*/ Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); while (ifs.hasMoreElements()) { NetworkInterface iface = ifs.nextElement(); if (iface.isUp()) { Enumeration<InetAddress> adds = iface.getInetAddresses(); while (adds.hasMoreElements()) { InetAddress addr = adds.nextElement(); if (!isIpV6(addr) && !addr.isAnyLocalAddress() && !addr.isLoopbackAddress()) { return addr.getHostAddress(); } } } } } catch (SocketException se) { se.printStackTrace(); } return "NOIP"; } private static boolean isIpV6(InetAddress addr) { return addr.getHostAddress().contains(":"); } }