title: No route to host : connect
body: <p>I am getting my public IP from the website, but sometimes I get this error:</p>
  
  <pre><code>java.net.NoRouteToHostException: No route to host: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at sun.net.NetworkClient.doConnect(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
      at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.URL.openStream(Unknown Source)
      at Connection.getIp(Connection.java:86)
      at Connection.setIP(Connection.java:31)
      at Connection.connect(Connection.java:67)
  </code></pre>
  
  <p>I guess it's because the system did not get the reply from the web yet, and cannot continue, but im sure that im wrong..</p>
  
  <p>Why does that happen? How can I 'fix' it?</p>
  
  <pre><code>public String getIp() throws IOException {
      URL whatismyip = new URL("http://icanhazip.com");
      BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
      return in.readLine();
  }
  </code></pre>
  
  <p>Well this is kind of a VPN management system, so I can reconnect and change the ip of the vpn, but I need to check if the ip has changed and thats how I do it and the error OCCURS from this method:</p>
  
  <pre><code>public boolean connect() throws IOException, InterruptedException {
      String ip = this.setIP();
      if (ip != null) {
          if (ip.equals(this.defaultIp)) {
              System.out.println("equals");
              previousIp = ip;
              HMACommand.connect();
              Thread.sleep(500);
              while (ip.equals(this.defaultIp) || ip == null) {
                  if (ip == null) {
                      System.out.println("Is null");
                      return connect();
                  }
                  ip = this.setIP();
                  Thread.sleep(75);
              }
  
              return true;
          }
          else {
              this.previousIp = ip;
              HMACommand.reconnect();
              Thread.sleep(500);
              while (ip.equals(this.defaultIp) || ip.equals(this.previousIp) || ip == null) {
                  if (ip == null) {
                      return connect();
                  }
                  ip = this.setIP();
                  Thread.sleep(75);
              }
              return true;
          }
      }
      else {
          boolean flag = connect();
          return (flag) ? flag : false;
      }
  }
  </code></pre>