Here you can find the source of getLocalAddress()
public static InetAddress getLocalAddress()
//package com.java2s; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class Main { public static InetAddress getLocalAddress() { InetAddress addr = null;//from ww w . j a va 2s. c o m try { Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); ip = (InetAddress) ni.getInetAddresses().nextElement(); if (!ip.isLoopbackAddress()) { addr = ip; break; } } addr = addr != null ? addr : InetAddress.getLocalHost(); } catch (Exception e) { } return addr; } }