java.net.SocketException: Connection reset ?

R

Roland

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.
 
D

Daniel Pitts

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.
 
R

Roland

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...
 
G

Gordon Beaton

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

--
 
E

Esmond Pitt

Roland said:
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.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top