views:

55

answers:

2

I have a connection that should recieve a heartbeat every two minutes.

However occasionally something happens and the network goes bad. For some reason Solaris believes that the connection is still alive, No IOException is raised locally, however one is raised remotely.

I have set a timeout on the socket, and when debugged from a Windows XP test environment we can confirm that after two minutes and ten seconds the socket times out as expected.

Is there any magical chant that I am not reciting that I need to in order for the exception to be raised?

public void run() {
    this.connect();
    while (true) {
        try {
            InputStreamReader reader = new InputStreamReader(this.socket.getInputStream());
            OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream(), "ISO-8859-1");
            for (int errorCount = 0; errorCount < 15;) {
                if (!this.readResponse(reader, writer)) {
                    errorCount++;
                }
            }
            this.logger.error("read incomplete message for 15 times, reset the connection");
        } catch (Exception e) {
            this.logger.error("read response error", e);
        } finally {
            this.reconnect();
        }
    }
}

Where

private boolean readResponse(InputStreamReader reader, OutputStreamWriter writer) throws IOException {
    int length = 0;
    for (int i = 0; i < 2; i++) {
        int ch = reader.read();
        if (ch < 0) {
            this.logger.error("read response buffer length error");
            return false;
        }
        length = length * 256 + ch;
    }
return true;
}

is the beginning of the readResponse method.

The method is blocking on int ch = reader.read(); in readResponse, as I would expect but the SocketTimeoutException is never being raised.

Any Ideas?

The code works in all cases except timeouts.

Thanks for any help.

A: 

I'm wondering if you could do something with:

if (reader.ready()) { int ch = reader.read(); }

And maybe a short timeout to sleep between calls of that method.

Dean J
A: 

Take a look at http://bugs.sun.com/view%5Fbug.do?bug%5Fid=6596323 and see if it addresses your problem

Jim Garrison