private static InetAddress findLocalAddressListeningOnPort ( String pHost , int pPort ) throws UnknownHostException , SocketException { InetAddress address = InetAddress . getByName ( pHost ) ; if ( address . isLoopbackAddress ( ) ) { // First check local address InetAddress localAddress = getLocalAddress ( ) ; if ( ! localAddress . isLoopbackAddress ( ) && isPortOpen ( localAddress , pPort ) ) { return localAddress ; } // Then try all addresses attache to all interfaces localAddress = getLocalAddressFromNetworkInterfacesListeningOnPort ( pPort ) ; if ( localAddress != null ) { return localAddress ; } } return address ; }
Check for an non - loopback local adress listening on the given port