Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SocketException and servletrunner

0 views
Skip to first unread message

Clifford Helsel

unread,
Jun 6, 1999, 3:00:00 AM6/6/99
to
I have included some bare minimum code which demonstrates an error I
am encountering while establishing a connection to the servletrunner
from within a Java program. The problem is that when my program
terminates I receive a stack trace from servletrunner as follows:

java.net.SocketException: Connection reset by peer
at java.net.SocketInputStream.read(SocketInputStream.java:84)
at
sun.servlet.http.HttpInputStream.fill(HttpInputStream.java:346)
at
sun.servlet.http.HttpInputStream.readLine(HttpInputStream.java:239)
at
sun.servlet.http.HttpRequestLine.readLine(HttpRequestLine.java:175)
at
sun.servlet.http.HttpRequestLine.parse(HttpRequestLine.java:148)
at sun.servlet.http.HttpRequest.next(HttpRequest.java:323)
at
sun.servlet.http.HttpServerHandler.handleConnection(HttpServerHandler.java:105)
at
sun.servlet.http.HttpServerHandler.run(HttpServerHandler.java:90)
at java.lang.Thread.run(Thread.java:466)

A solution I have found is to cast the URLConnection returned by
url.openConnection (in the code below) to an HttpURLConnection and
before
terminating, call con.disconnect(). The disconnect() method resides in
the
HttpURLConnection class. However, I don't view this as an
acceptable solution since I am not guaranteed that url.openConnection()
will always give me an HttpURLConnection.

import java.net.*;
import java.io.*;

public class ConnectionTest {
public static void main(String[] args) {
try {
URL url=new URL("http://localhost:8080");
URLConnection con=url.openConnection();
con.setDoOutput(true);
DataOutputStream dataOut=new DataOutputStream(con.getOutputStream());
} catch (Exception e){
System.out.println(e);
}
}
}

You can test this by invoking servletrunner and then compiling and
executing the sample program above. Any ideas?

Cliff Helsel
che...@spacelab.net

William Brogden

unread,
Jun 6, 1999, 3:00:00 AM6/6/99
to
> I have included some bare minimum code which demonstrates an error I
> am encountering while establishing a connection to the servletrunner
> from within a Java program. The problem is that when my program
> terminates I receive a stack trace from servletrunner as follows:

snip snip

It seems to me that this is not an error but normal behavior.
Why would you expect anything different if the socket is still
open when your program terminates?

WBB

Clifford Helsel

unread,
Jun 6, 1999, 3:00:00 AM6/6/99
to William Brogden
My point is that the URLConnection class provides an openConnection
method, however, there is no closeConnection method or disconnect
method except in the subclass HttpURLConnection. Since URLConnection.
openConnection() does not guarantee that you have an HttpURLConnection
then you can not rely on calling disconnect in the HttpURLConnection
subclass. A workaround is to test instanceof and cast but this seems
kludgy.

Cliff Helsel

William Brogden

unread,
Jun 6, 1999, 3:00:00 AM6/6/99
to
Clifford Helsel wrote:
>
> My point is that the URLConnection class provides an openConnection
> method, however, there is no closeConnection method or disconnect
> method except in the subclass HttpURLConnection. Since URLConnection.
> openConnection() does not guarantee that you have an HttpURLConnection
> then you can not rely on calling disconnect in the HttpURLConnection
> subclass. A workaround is to test instanceof and cast but this seems
> kludgy.
>
> Cliff Helsel

I think that all you have to do is close the Input and Output Streams
to destroy the connection gracefully.

WBB

0 new messages