6

I am trying to sort through a list of SOCKS proxies, and figure out which ones have a connect and read time of less than 1000ms, here is my code

for(Proxy p : proxies) {
            try {
            URLConnection testConnection = testUrl.openConnection(p);
            testConnection.setConnectTimeout(TIMEOUT_VALUE);
            testConnection.setReadTimeout(TIMEOUT_VALUE);
            success.add(p);
            } catch(SocketTimeoutException ste) {
                System.out.println("Proxy " + p.address().toString() + " timed out.");
            }
        }

But every single one of them passes the test, even when I do TIMEOUT_VALUE = 1; What am I doing wrong? Thanks for any help.

Austin
  • 4,801
  • 6
  • 34
  • 54
  • Can you add this line and see what the value it returns? `testConnection.getConnectTimeout()` – CoolBeans Jun 30 '11 at 03:53
  • I meant add this after you set the timeout value. – CoolBeans Jun 30 '11 at 04:00
  • @CoolBeans It just prints out 1000, just like it's supposed to. – Austin Jun 30 '11 at 04:01
  • I saw this on [java docs](http://download.oracle.com/javase/6/docs/api/java/net/URLConnection.html#setConnectTimeout%28int%29) _Some non-standard implmentation of this method may ignore the specified timeout_. I am not sure what are some of these _non-standard_ implementations are though. – CoolBeans Jun 30 '11 at 04:10

2 Answers2

13

I assume your problem is you don't read anything from connection. If I set TIMEOUT_VALUE too low, I get an exception. Whether I read all response or only one line did not affect the resulting time, I guess it is because I got whole answer in one packet.

Here is the measurement I used (without proxies):

    int TIMEOUT_VALUE = 1000;
    try {
        URL testUrl = new URL("http://google.com");
        StringBuilder answer = new StringBuilder(100000);

        long start = System.nanoTime();

        URLConnection testConnection = testUrl.openConnection();
        testConnection.setConnectTimeout(TIMEOUT_VALUE);
        testConnection.setReadTimeout(TIMEOUT_VALUE);
        BufferedReader in = new BufferedReader(new InputStreamReader(testConnection.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            answer.append(inputLine);
            answer.append("\n");
        }
        in.close();

        long elapsed = System.nanoTime() - start;
        System.out.println("Elapsed (ms): " + elapsed / 1000000);
        System.out.println("Answer:");
        System.out.println(answer);
    } catch (SocketTimeoutException e) {
        System.out.println("More than " + TIMEOUT_VALUE + " elapsed.");
    }
Thresh
  • 943
  • 14
  • 23
  • 1
    Thanks this seemed to work, for added measures I check if the elapsed time was less than the timeout value before I added it to the success list. Thanks again! – Austin Jun 30 '11 at 20:01
0

I use

    System.getProperties().put("proxySet", "true");
    System.getProperties().put("http.proxyHost", "192.168.10.121");
    System.getProperties().put("http.proxyPort", "8080");
    //System.getProperties().put("http.nonProxyHosts", "8080");
Ronald Coarite
  • 4,460
  • 27
  • 31