Here you can find the source of getStrLocalIP()
public static String getStrLocalIP() throws SocketException, UnknownHostException
//package com.java2s; //License from project: Apache License import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class Main { public static String getStrLocalIP() throws SocketException, UnknownHostException { String ip = null;/* w ww.java 2s . c o m*/ Enumeration<NetworkInterface> enuNI = NetworkInterface.getNetworkInterfaces(); begin: while (enuNI.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) enuNI.nextElement(); Enumeration<InetAddress> enuAddress = ni.getInetAddresses(); while (enuAddress.hasMoreElements()) { InetAddress address = enuAddress.nextElement(); if (!address.isSiteLocalAddress() && !address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1) { ip = address.getHostAddress(); break begin; } } } if (ip == null) { InetAddress addr = InetAddress.getLocalHost(); ip = addr.getHostAddress(); } return ip; } }