Simple code, complex problem: "Software caused connection abort: recv failed"

T

taras.di

Hi everyone.

I'm trying to write code that takes input from a text file, writes it
to a socket connected to a webserver (that I have written as part of a
university assignment), then reads the output of the webserver - sounds
simple enough (especially with the abstraction provided by java).
However, when I run the code, a SocketException is thrown. I'm quite
sure that the webserver isn't a fault - its output guarantees that the
correct response is being sent. Also, connecting to the webserver
using a broswer or telnet shows no problems. The code for the test
bench is shown below:

Socket socket = new Socket("127.0.0.1", 9836);
DataOutputStream outToClient = new DataOutputStream
(socket.getOutputStream());
BufferedReader inFromClient = new BufferedReader
(new InputStreamReader(socket.getInputStream()));
outToClient.writeBytes("GET index.html\r\n");
String toPrint = inFromClient.readLine(); // this causes
the EXCEPTION to be thrown
System.out.println(toPrint);

Looks easy enough. When I step through the code using JBuilder's
debugger, all is well. Sometimes, no Exception is thrown when the code
is run through at full pace, but most of the time the exception is
thrown. The output is shown below:

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
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)
at TestBench.main(TestBench.java:31)

I've browsed the web for a couple of hours looking for answers, but
they all seem to be regarding SQL/Apache/Tomcat (not simple java code).
Any gurus have an idea of what might be happening?

Thanks

Taras
 
S

shakah

Hi everyone.

I'm trying to write code that takes input from a text file, writes it
to a socket connected to a webserver (that I have written as part of a
university assignment), then reads the output of the webserver - sounds
simple enough (especially with the abstraction provided by java).
However, when I run the code, a SocketException is thrown. I'm quite
sure that the webserver isn't a fault - its output guarantees that the
correct response is being sent. Also, connecting to the webserver
using a broswer or telnet shows no problems. The code for the test
bench is shown below:

Socket socket = new Socket("127.0.0.1", 9836);
DataOutputStream outToClient = new DataOutputStream
(socket.getOutputStream());
BufferedReader inFromClient = new BufferedReader
(new InputStreamReader(socket.getInputStream()));
outToClient.writeBytes("GET index.html\r\n");
String toPrint = inFromClient.readLine(); // this causes
the EXCEPTION to be thrown
System.out.println(toPrint);

Looks easy enough. When I step through the code using JBuilder's
debugger, all is well. Sometimes, no Exception is thrown when the code
is run through at full pace, but most of the time the exception is
thrown. The output is shown below:

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
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)
at TestBench.main(TestBench.java:31)

I've browsed the web for a couple of hours looking for answers, but
they all seem to be regarding SQL/Apache/Tomcat (not simple java code).
Any gurus have an idea of what might be happening?

Thanks

Taras

I'm not that familiar with the rules for "simple" HTTP requests (v
0.9?), but are you missing the terminating empty line in the request
header? Do you see the same behavior if you issue HTTP/1.0 requests and
terminate the request header properly with an empty new line, i.e.:

outToClient.writeBytes("GET index.html HTTP/1.0\r\n\r\n") ;
 
T

Thomas Hawtin

I'm not that familiar with the rules for "simple" HTTP requests (v
0.9?), but are you missing the terminating empty line in the request
header? Do you see the same behavior if you issue HTTP/1.0 requests and
terminate the request header properly with an empty new line, i.e.:

outToClient.writeBytes("GET index.html HTTP/1.0\r\n\r\n") ;

The rules for HTTP 0.9 is that no-one has supported it for years. You
probably want to suppply a Host: line too. Virtual web servers are a tad
common these days.

Shouldn't it be /index.html? Relative URLs don't make a great deal of
sense in this context.

Tom Hawtin
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top