Here you can find the source of getLocalAddressAsString()
private static String getLocalAddressAsString() throws UnknownHostException, SocketException
//package com.java2s; //License from project: Open Source License import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class Main { private static String getLocalAddressAsString() throws UnknownHostException, SocketException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces != null && interfaces.hasMoreElements()) { Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses(); while (addresses != null && addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (acceptableAddress(address)) { return address.getHostAddress(); }//w w w . j a v a 2 s . c o m } } throw new UnknownHostException(); } private static boolean acceptableAddress(InetAddress address) { return address != null && !address.isLoopbackAddress() && !address.isAnyLocalAddress() && !address.isLinkLocalAddress(); } }