Help:java.net.SocketException: Connection reset

Discussion in 'Java' started by ckumar, Feb 23, 2005.

  1. ckumar

    ckumar Guest

    Hai,

    When I tried to read a HTML page using URLConnection I got the following
    Exception. I need help in solving this problem.

    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:220)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:277)
    at
    sun.net.www.http.ChunkedInputStream.fastRead(ChunkedInputStream.java:221)
    at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:662)
    at java.io.FilterInputStream.read(FilterInputStream.java:111)
    at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)
    at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)

    Thanx in advance.

    Regards,
    ackumar
     
    ckumar, Feb 23, 2005
    #1
    1. Advertising

  2. "ckumar" <> wrote in message
    news:...
    > Hai,
    >
    > When I tried to read a HTML page using URLConnection
    > I got the following Exception. I need help in solving this
    > problem.
    >
    > java.net.SocketException: Connection reset
    > at java.net.SocketInputStream.read(SocketInputStream.java:168)
    > at java.io.BufferedInputStream.read1(BufferedInputStream.java:220)
    > at java.io.BufferedInputStream.read(BufferedInputStream.java:277)

    <SNIP>
    >


    Supplying a compilable snippet of the relevant source code makes problem
    diagnosis easier [not to mention better than guesswork :)].

    I hope this helps.

    Anthony Borla
     
    Anthony Borla, Feb 23, 2005
    #2
    1. Advertising

  3. ckumar

    ckumar Guest

    Hai,

    Thanx for replying. I'll explain the code first .

    The code recursively reads the online pages and writes into a file. This
    readPage method is recursively. This code work fine, but for some pages it
    gives the exception (given in previous post) and jumps to next page.

    readPage(Sring url,String tempfile)
    {
    try{
    String line="";
    byte[] buf;
    FileOutputStream fo = new FileOutputStream(tempfile);
    URLConnection con = new URL(url).openConnection();
    BufferedReader br = new BufferedReader(new
    InputStreamReader(con.getInputStream()));
    while(((line = br.readLine()) != null)) {
    line = line + '\n';
    buf = line.getBytes();
    fo.write(buf);
    line = "";
    }
    br.close();
    }catch(Exception ee){ee.printStackTrace();}
    }


    Regards,
    ackumar
     
    ckumar, Feb 23, 2005
    #3
  4. ckumar wrote:

    > Hai,
    >
    > Thanx for replying. I'll explain the code first .
    >
    > The code recursively reads the online pages and writes into a file. This
    > readPage method is recursively. This code work fine, but for some pages it
    > gives the exception (given in previous post) and jumps to next page.


    Does the code fail on the same URLs every time? Have you checked that
    you can successfully browse to the URLs on which it fails?

    > readPage(Sring url,String tempfile)
    > {
    > try{
    > String line="";
    > byte[] buf;
    > FileOutputStream fo = new FileOutputStream(tempfile);
    > URLConnection con = new URL(url).openConnection();
    > BufferedReader br = new BufferedReader(new
    > InputStreamReader(con.getInputStream()));


    The above part is faulty: it uses the system's default charset to
    convert the input byte stream to a character stream, without any thought
    to whether it's the correct charset -- or indeed, any thought as to
    *whether the data is character data at all*.

    > while(((line = br.readLine()) != null)) {
    > line = line + '\n';
    > buf = line.getBytes();
    > fo.write(buf);


    The above part is also faulty: it adds a specific line separator
    sequence to the line (rather than either the appropriate one for the
    platform or the one that was read from the input) and it converts from
    characters back to bytes using the system's default character encoding.
    The round trip bytes -> characters -> bytes can lose data, and the
    line terminator manipulation can garble data, especially when coupled
    with the character -> bytes business.

    > line = "";


    And what does that do for you?

    > }
    > br.close();


    The reader should be closed in a finally {} block, to ensure that it
    _is_ closed.

    > }catch(Exception ee){ee.printStackTrace();}
    > }


    Converting from bytes to chars back to bytes is foolish because the
    appropriate details of the conversions can be tricky to work out in the
    general case, and because it doesn't need to be done at all. Use the
    InputStream directly, reading the bytes into a buffer array and then
    writing them back out to the OutputStream, until end of stream is reached.

    It may be the case that none of that addresses the problem you actually
    asked about. There are circumstances beyond your control, such as
    server faults and intentional reboots, network interruptions, and
    various other potential problems that might cause your download to fail.
    Your program cannot avoid them except by luck, so it needs to have a
    strategy for dealing with them.

    --
    John Bollinger
     
    John C. Bollinger, Feb 23, 2005
    #4
  5. ckumar

    ckumar Guest

    Hai John,

    Abt my code, I sent only a part of a code. The code is working fine.And It
    doen't fail on the same URLs every time.

    When I quit my program and run again I can read that same URL but give the
    exception for some other URL.

    Regards,
    ackumar
     
    ckumar, Feb 24, 2005
    #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. bigBrother82

    SocketException: Connection reset

    bigBrother82, Nov 5, 2005, in forum: Java
    Replies:
    1
    Views:
    4,459
    Roedy Green
    Nov 5, 2005
  2. JPractitioner
    Replies:
    2
    Views:
    1,102
    Gordon Beaton
    Jan 18, 2006
  3. Replies:
    0
    Views:
    1,612
  4. Roland
    Replies:
    4
    Views:
    16,063
    Esmond Pitt
    May 3, 2007
  5. fb1800
    Replies:
    0
    Views:
    969
    fb1800
    Dec 8, 2010
Loading...

Share This Page