Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java tutorial sample code throws: java.net.SocketException: Connection reset - what does cause it?

Tags:

java

exception

So this is a beginner's question.

When executing the sample code from the working with urls chapter it throws:

Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) ...

Origin is the openStream() method.

Here is the code:

    import java.net.*;
    import java.io.*;

    public class URLReader {

       public static void main(String[] args) throws Exception {

          URL oracle = new URL("http://www.oracle.com/");
          BufferedReader in = new BufferedReader(
          new InputStreamReader(oracle.openStream()));

          String inputLine;
          while ((inputLine = in.readLine()) != null) {
             System.out.println(inputLine);
          }
          in.close();
       }
    }

I know there are similar threads regarding that topic, but i could not find an answer that suits me.

What I've tried so far:

  • I have set the proxy host as suggested here. Command was: java -Dhttp.proxyHost=dslb-088-071-100-199.pools.arcor-ip.net, I also tried it with inserting System.setProperty("http.proxyHost", "dslb-088-071-100-199.pools.arcor-ip.net"); in the first line of the URLReader class.
  • I tried JSoup html parser and
  • org.apache.commons.io.FileUtils.copyURLToFile(URL, File) method to have a similar result.

Whatever I try, I always get the same error: There will happen nothing for 30 seconds or so and then it throws the mentioned SocketException.

I simply dont know how to continue in solving this problem. Helpful would be to get information about what happens in background during the 30seconds before connection reset.

So what could actually cause this Exception?

The smallest hint could help! Thank you!

like image 299
achingfingers Avatar asked Jun 05 '13 14:06

achingfingers


1 Answers

Your code is working fine for JVM's that can connect to the internet.

Based on the original question and discussion: https://chat.stackoverflow.com/rooms/31264/discussion-between-achingfingers-and-meewok it seems that either:

  • An intermediate firewall is blocking the JVM from making the connection (or another similar network issue).
  • An operating system firewall, or antivirus that is causing the problems as well.

My suggestion is to try:

  • Same app on different computer within same network (to see if it is PC specific).
  • Same app on different network.
like image 139
6 revs Avatar answered Oct 30 '22 00:10

6 revs