0

I have an application to write the request data over socket and read the response.

Some times Host doesn't respond to request and my code blocks, even though I'm using a read timeout.

There is no way to clear it manually and it require system to be rebooted or restart on the server handler required.

Here is the code used. Connection appears to block at objBufferedInputStream.read(..)

Socket clientSocket = new Socket(objInetAddress, hostPort);
clientSocket.setKeepAlive(true);
clientSocket.setReceiveBufferSize(8192);
clientSocket.setSendBufferSize(8192);
clientSocket.setSoTimeout(waitTimeBeforeSocketClose * 1000);
objBufferedInputStream = new BufferedInputStream(clientSocket
                    .getInputStream());
.......
objBufferedOutputStream.write(message, 0, message.length);
objBufferedOutputStream.flush();

while (bytesLeft > 0 && bytesread > -1) {
        try {
            bytesread = objBufferedInputStream.read(data, 4096 - bytesLeft, 1);             
        } catch (IOException e) {
            objLogger.writeException(e);
            try {
                objBufferedInputStream.close();
            } catch (IOException e1) {
                objLogger.writeException(e1);

                return null;
            }
            return null;
        }
        bytesLeft -= bytesread;
    }
    return data;

}

Please advise whether this is expected behavior when the host doesn't respond or hold the response.

Please advise whether there is alternate approach.

2
  • please read stackoverflow.com/help/how-to-ask and stackoverflow.com/help/mcve Commented Feb 17, 2015 at 15:47
  • XY problem. "Please advise whether this is expected behavior when the host doesn't respond or hold the response" and "please advise whether there is [an] alternate approach" both assume your code is correct. It isn't. Commented Feb 17, 2015 at 16:27

1 Answer 1

1

Either:

  1. waitTimeBeforeSocketClose is zero, so you are blocking indefinitely, or

  2. It is non-zero, so you're getting a SocketTimeoutException inside your loop, and ignoring it, so you spin forever. Add a separate catch block for SocketTimeoutException with a break; inside it. Never just ignore an IOException. In this case I would say you should exit the loop on any IOException, so you could also consider putting the try/catch outside the loop.

Reading one byte at a time is pretty poor. I don't see the point of that.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, Thanks. I have updated the source code in the original post. We exit the loop when there is IOException. There is no exception thrown. I have even added logging before read method and it is printed only once and then there is no more action. This socket read/write happens with AS400 system to perform transactions.
So what exactly is the value of waitTimeBeforeSocketClose?
It is set to 30 and application multiplies it by 1000 to make it 30 seconds.
I have made the changes to use read available to fix this issue. Please advise whether available is fine instead of specifying the number of bytes. this change make it faster as it read all the values in two to three iterations objBufferedInputStream.read(data, 4096 - bytesLeft, objBufferedInputStream.available)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.