java.net.SocketException: Connection reset ?

Discussion in 'Java' started by Roland, May 2, 2007.

  1. Roland

    Roland Guest

    Hello. Look on my code, which can get WWW pages: (for example
    http://whatismyip.com/)

    InetSocketAddress socketAddress = new InetSocketAddress(new
    URL("http://whatismyip.com/").getHost(), 80);
    Socket socket = new Socket();
    socket.connect(socketAddress);

    BufferedWriter writer = new BufferedWriter(new
    OutputStreamWriter(socket.getOutputStream()));
    String request = "GET / HTTP/1.1\r\nUser-Agent: Opera/9.20
    (Windows NT 5.0; U; pl)\r\nHost: whatismyip.com\r\n\r\n";
    writer.write(request);
    writer.flush();

    StringBuffer content = new StringBuffer();
    String line;
    BufferedReader reader = new BufferedReader(new
    InputStreamReader(socket.getInputStream()));
    while ((line = reader.readLine ()) != null)
    content.append(line).append ("\n");
    String response = content.toString();
    System.out.println(response);

    Unfortunately, there is a error:
    java.net.SocketException: Connection reset
    in this line:
    while ((line = reader.readLine()) != null)

    When I download others pages, everything is OK, but this code always
    cannot get http://whatismyip.com/ page. Why?
    Where is an error?

    Thanks for your answer.
    Roland, May 2, 2007
    #1
    1. Advertising

  2. Roland

    Daniel Pitts Guest

    On May 2, 7:27 am, Roland <> wrote:
    > Hello. Look on my code, which can get WWW pages: (for examplehttp://whatismyip.com/)
    >
    > InetSocketAddress socketAddress = new InetSocketAddress(new
    > URL("http://whatismyip.com/").getHost(), 80);
    > Socket socket = new Socket();
    > socket.connect(socketAddress);
    >
    > BufferedWriter writer = new BufferedWriter(new
    > OutputStreamWriter(socket.getOutputStream()));
    > String request = "GET / HTTP/1.1\r\nUser-Agent: Opera/9.20
    > (Windows NT 5.0; U; pl)\r\nHost: whatismyip.com\r\n\r\n";
    > writer.write(request);
    > writer.flush();
    >
    > StringBuffer content = new StringBuffer();
    > String line;
    > BufferedReader reader = new BufferedReader(new
    > InputStreamReader(socket.getInputStream()));
    > while ((line = reader.readLine ()) != null)
    > content.append(line).append ("\n");
    > String response = content.toString();
    > System.out.println(response);
    >
    > Unfortunately, there is a error:
    > java.net.SocketException: Connection reset
    > in this line:
    > while ((line = reader.readLine()) != null)
    >
    > When I download others pages, everything is OK, but this code always
    > cannot gethttp://whatismyip.com/page. Why?
    > Where is an error?
    >
    > Thanks for your answer.



    Why not use:
    new URL("http://whatismyip.com/").getConnection() or getContent even?
    Its easier than dealing with the HTTP protocol yourself.
    Daniel Pitts, May 2, 2007
    #2
    1. Advertising

  3. Roland

    Roland Guest

    Thanks. When I use:
    URLConnection con = url.openConnection();
    it is OK, but:
    -firstly: why use a socket to get page throws exception? Is it an
    error in java platform?
    -secondly: how can I abort getting pages? If I want to stop
    downloading page, I can use:
    socket.close();
    but in URLConnection class, there is not close() or abort() method...
    Roland, May 2, 2007
    #3
  4. On 2 May 2007 07:27:16 -0700, Roland wrote:
    > When I download others pages, everything is OK, but this code always
    > cannot get http://whatismyip.com/ page. Why?
    > Where is an error?


    Your code works for me, more or less.

    Because your read loop reads to EOF and HTTP 1.1 by default uses
    persistent connections, the server doesn't close the connection after
    sending the response and the loop blocks until the server eventually
    times out the now idle connection. You need to request Connection:
    close or obey the Content-Length response header to know when to stop
    reading.

    Connection reset is typically sent by overloaded Windows servers, and
    the host you're having problems with is running Windows. It worked
    when I tried it.

    However instead of implementing this myself I'd use URLConnection, as
    others have suggested.

    /gordon

    --
    Gordon Beaton, May 3, 2007
    #4
  5. Roland

    Esmond Pitt Guest

    Roland wrote:

    > Thanks. When I use:
    > URLConnection con = url.openConnection();
    > it is OK, but:
    > -firstly: why use a socket to get page throws exception? Is it an
    > error in java platform?


    No, it's a nasty Web server resetting your connection instead of closing
    it properly. It seems that URLConnection copes with that internally,
    whereas Socket throws it to you.

    > but in URLConnection class, there is not close() or abort() method...


    Close the streams.
    Esmond Pitt, May 3, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ckumar
    Replies:
    4
    Views:
    41,953
    ckumar
    Feb 24, 2005
  2. bigBrother82

    SocketException: Connection reset

    bigBrother82, Nov 5, 2005, in forum: Java
    Replies:
    1
    Views:
    4,453
    Roedy Green
    Nov 5, 2005
  3. JPractitioner
    Replies:
    2
    Views:
    1,101
    Gordon Beaton
    Jan 18, 2006
  4. Replies:
    0
    Views:
    1,610
  5. fb1800
    Replies:
    0
    Views:
    963
    fb1800
    Dec 8, 2010
Loading...

Share This Page