Help:java.net.SocketException: Connection reset

C

ckumar

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
 
A

Anthony Borla

ckumar said:
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)

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
 
C

ckumar

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
 
J

John C. Bollinger

ckumar said:
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.
 
C

ckumar

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top