Here you can find the source of getInetAddress()
private static InetAddress getInetAddress()
//package com.java2s; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Collections; import java.util.Enumeration; public class Main { private static InetAddress getInetAddress() { InetAddress currentAddress = null; try {//from w w w. ja va 2s. c o m InetAddress addrs[] = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) { currentAddress = addr; break; } } if (currentAddress == null || currentAddress.getHostAddress() == null || currentAddress.getHostAddress().length() == 0) { try { Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface iface : Collections.list(ifaces)) { Enumeration<InetAddress> raddrs = iface.getInetAddresses(); for (InetAddress raddr : Collections.list(raddrs)) { if (!raddr.isLoopbackAddress() && raddr.isSiteLocalAddress()) { currentAddress = raddr; break; } } } } catch (SocketException e) { e.printStackTrace(); } } } catch (UnknownHostException e) { } if (currentAddress != null && currentAddress.getHostAddress() != null && currentAddress.getHostAddress().length() > 0) { return currentAddress; } return null; } }