Intermittent SocketException

Hello,

I have a strange intermittent socket error occuring; stacktrace
follows:

23:22:01|1|java.net.SocketException (Socket operation on nonsocket:
recv failed)
23:22:01|1| at java.net.SocketInputStream.socketRead0(Native
Method)
23:22:01|1| at
java.net.SocketInputStream.read(SocketInputStream.java:129)
23:22:01|1| at
sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)
23:22:01|1| at
sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)
23:22:01|1| at
sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)
23:22:01|1| at
java.io.InputStreamReader.read(InputStreamReader.java:167)
23:22:01|1| at java.io.BufferedReader.fill(BufferedReader.java:136)
23:22:01|1| at
java.io.BufferedReader.readLine(BufferedReader.java:299)
23:22:01|1| at
java.io.BufferedReader.readLine(BufferedReader.java:362)

Here is a code snippet that is causing it:

ServerSocket server = new ServerSocket (PORT);
Socket socket = server.accept();
BufferedReader br = new BufferedReader (new
InputStreamReader(socket.getInputStream()));
String s = br.readLine();

The error occurs when attempting to read from the SocketInputStream on
the last line.

Oddly, although this Exception is thrown on the server and I'm
subsequently closing the client Connection in a finally clause, the
client does not seem to notice that anything is wrong, and so waits
indefinitely for the remainder of the conversation.

Any tips on how to avoid this, or what may be causing it?

Thanks in advance :)

Matt Pryor
[1757 byte] By [mattpryor] at [2007-11-19 18:22:27]
# 1 Re: Intermittent SocketException
ServerSocket server = new ServerSocket (PORT);
Socket socket = server.accept();
BufferedReader br = new BufferedReader (new
InputStreamReader(socket.getInputStream()));
String s = br.readLine();

Is this code you actually use, or an example? I can't find logic faults in examples, I need to see your code that's causing the exception.

br.readLine() on a live socket with nothing to read should block, and not cause an exception, which means the socket is actually dead and you are trying to read from it. This in-and-of itself is not neccesairly abnormal. Unexpected connection resets happen. Catch it and deal with it programmatically.

Oddly, although this Exception is thrown on the server and I'm
subsequently closing the client Connection in a finally clause, the
client does not seem to notice that anything is wrong, and so waits
indefinitely for the remainder of the conversation.

The fault may very well reside in the client closing the connection before you think it does.
pwtenny at 2007-11-10 2:20:59 >