public static InetAddress getLocalAddress ( ) throws UnknownHostException , SocketException { InetAddress addr = InetAddress . getLocalHost ( ) ; NetworkInterface nif = NetworkInterface . getByInetAddress ( addr ) ; if ( addr . isLoopbackAddress ( ) || addr instanceof Inet6Address || nif == null ) { // Find local address that isn't a loopback address InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface ( ) ; // If a local, multicast enabled address can be found, use it. Otherwise // we are using the local address, which might not be what you want addr = lookedUpAddr != null ? lookedUpAddr : InetAddress . getByName ( "127.0.0.1" ) ; } return addr ; }
Get a local IP4 Address preferable a non - loopback address which is bound to an interface .