0

I've wrote a code that open Tor. I also have a list of exit nodes that automatically update the torrc file. Sometime, the ip is not working but tor is still loading trying to connect with it. Is there any way to check or have a return if the IP is not working or too long to load a page ? (P.S: sorry for my english, I'm french)

Thank you for your help.

public void openTor1()
{


}

1 Answers1

0

You can use this code to check if a host is reachable:

import java.net.InetAddress;

private boolean isHostReachable(String host)
{
    try
    {
        return InetAddress.getByName(host).isReachable(500);
    }
    catch (IOException e)
    {
        System.out.err("Error while checking if host is reachable: " + host + ", error is: " + e.getMessage());
        return false;
    }
}

If needed, you can check connection speed to that host. You'll need some HttpClient / Connection object for that. Idea is to send Http request, wait for response and measure elapsed time. Pseudocode to do it:

     long startTime = System.currentTimeMillis();

     //Write this above before your httprequest 

          HttpResponse httpResponse = httpClient.execute(httpGet);
          HttpEntity httpEntity = httpResponse.getEntity();
          is = httpEntity.getContent();    

    //After you get response  

    long elapsedTime = System.currentTimeMillis() - startTime;
    System.out.println("Total elapsed http request/response time in milliseconds: " + elapsedTime);

Basic tutorial for Http messaging: Using java.net.URLConnection to fire and handle HTTP requests

Community
  • 1
  • 1
Ivan Pronin
  • 1,587
  • 13
  • 12
  • Hi, thank you , it works very well. Is there a parameter that I can add too check if the connection to this host is not too slow ? Because sometimes, the host (exit node) is working but it is just too slow too load any webpages. – vinscarter Apr 03 '17 at 20:05
  • I've added this issue to my answer. Hope that helps! – Ivan Pronin Apr 03 '17 at 20:34