public static InetAddress findConnectingAddress(InetSocketAddress targetAddress,long maxWaitMillis,long startLoggingAfter) throws IOException {
  if (targetAddress == null) {
    throw new NullPointerException("targetAddress must not be null");
  }
  if (maxWaitMillis <= 0) {
    throw new IllegalArgumentException("Max wait time must be positive");
  }
  final long startTime=System.currentTimeMillis();
  long currentSleepTime=MIN_SLEEP_TIME;
  long elapsedTime=0;
  while (elapsedTime < maxWaitMillis) {
    AddressDetectionState strategy=AddressDetectionState.LOCAL_HOST;
    boolean logging=elapsedTime >= startLoggingAfter;
    if (logging) {
      LOG.info("Trying to connect to " + targetAddress);
    }
    do {
      InetAddress address=findAddressUsingStrategy(strategy,targetAddress,logging);
      if (address != null) {
        return address;
      }
switch (strategy) {
case LOCAL_HOST:
        strategy=AddressDetectionState.ADDRESS;
      break;
case ADDRESS:
    strategy=AddressDetectionState.FAST_CONNECT;
  break;
case FAST_CONNECT:
strategy=AddressDetectionState.SLOW_CONNECT;
break;
case SLOW_CONNECT:
strategy=null;
break;
default :
throw new RuntimeException("Unsupported strategy: " + strategy);
}
}
 while (strategy != null);
elapsedTime=System.currentTimeMillis() - startTime;
long toWait=Math.min(maxWaitMillis - elapsedTime,currentSleepTime);
if (toWait > 0) {
if (logging) {
LOG.info("Could not connect. Waiting for {} msecs before next attempt",toWait);
}
 else {
LOG.debug("Could not connect. Waiting for {} msecs before next attempt",toWait);
}
try {
Thread.sleep(toWait);
}
 catch (InterruptedException e) {
throw new IOException("Connection attempts have been interrupted.");
}
}
currentSleepTime=Math.min(2 * currentSleepTime,MAX_SLEEP_TIME);
}
LOG.warn("Could not connect to {}. Selecting a local address using heuristics.",targetAddress);
InetAddress heuristic=findAddressUsingStrategy(AddressDetectionState.HEURISTIC,targetAddress,true);
if (heuristic != null) {
return heuristic;
}
 else {
LOG.warn("Could not find any IPv4 address that is not loopback or link-local. Using localhost address.");
return InetAddress.getLocalHost();
}
}

public static InetAddress findConnectingAddress ( InetSocketAddress targetAddress , long maxWaitMillis , long startLoggingAfter ) throws IOException { if ( targetAddress == null ) { throw new NullPointerException ( "targetAddress must not be null" ) ; } if ( maxWaitMillis <= 0 ) { throw new IllegalArgumentException ( "Max wait time must be positive" ) ; } final long startTime = System . currentTimeMillis ( ) ; long currentSleepTime = MIN_SLEEP_TIME ; long elapsedTime = 0 ; while ( elapsedTime < maxWaitMillis ) { AddressDetectionState strategy = AddressDetectionState . LOCAL_HOST ; boolean logging = elapsedTime >= startLoggingAfter ; if ( logging ) { LOG . info ( "Trying to connect to " + targetAddress ) ; } do { InetAddress address = findAddressUsingStrategy ( strategy , targetAddress , logging ) ; if ( address != null ) { return address ; } switch ( strategy ) { case LOCAL_HOST : strategy = AddressDetectionState . ADDRESS ; break ; case ADDRESS : strategy = AddressDetectionState . FAST_CONNECT ; break ; case FAST_CONNECT : strategy = AddressDetectionState . SLOW_CONNECT ; break ; case SLOW_CONNECT : strategy = null ; break ; default : throw new RuntimeException ( "Unsupported strategy: " + strategy ) ; } } while ( strategy != null ) ; elapsedTime = System . currentTimeMillis ( ) - startTime ; long toWait = Math . min ( maxWaitMillis - elapsedTime , currentSleepTime ) ; if ( toWait > 0 ) { if ( logging ) { LOG . info ( "Could not connect. Waiting for {} msecs before next attempt" , toWait ) ; } else { LOG . debug ( "Could not connect. Waiting for {} msecs before next attempt" , toWait ) ; } try { Thread . sleep ( toWait ) ; } catch ( InterruptedException e ) { throw new IOException ( "Connection attempts have been interrupted." ) ; } } currentSleepTime = Math . min ( 2 * currentSleepTime , MAX_SLEEP_TIME ) ; } LOG . warn ( "Could not connect to {}. Selecting a local address using heuristics." , targetAddress ) ; InetAddress heuristic = findAddressUsingStrategy ( AddressDetectionState . HEURISTIC , targetAddress , true ) ; if ( heuristic != null ) { return heuristic ; } else { LOG . warn ( "Could not find any IPv4 address that is not loopback or link-local. Using localhost address." ) ; return InetAddress . getLocalHost ( ) ; } }
finds the local network address from which this machine can connect to the target address . this method tries to establish a proper network connection to the given target , so it only succeeds if the target socket address actually accepts connections . the method tries various strategies multiple times and uses an exponential backoff timer between tries . < p > if no connection attempt was successful after the given maximum time , the method will choose some address based on heuristics ( excluding link - local and loopback addresses . ) < p > this method will initially not log on info level ( to not flood the log while the backoff time is still very low ) . it will start logging after a certain time has passes .
