【问题标题】:delayed response received in java socket在 Java 套接字中收到延迟响应
【发布时间】:2014-01-17 12:06:11
【问题描述】:

我在 as400 和 java 连接方面需要帮助。

我在 SocketTimeoutExceptions 期间收到延迟响应时遇到问题。那么发生的情况是,当我发送第一个请求并发生响应超时时,在发送我的第二个请求后,收到第一个请求的响应,这会产生差异。

有没有办法在超时异常之后,响应不应该缓存或者不应该在下一次请求时被接收?

下面是我连接as400服务器的sn-p代码:

public synchronized static byte[] sendRequestAndGetResponse(byte[] requestBytes) {
    try {
        if(checkHostConnection()){
            LOG.debug("Sending request bytes to Host: {}", Hexifier.toHex(requestBytes));
            IOUtils.write(requestBytes, dos);
            long startTime = System.currentTimeMillis();
            LOG.info("Request sent to host.");

            LOG.info("Waiting on host response");
            byte[] responseLengthBuffer = new byte[4];
            IOUtils.readFully(dis, responseLengthBuffer);

            ByteBuffer bb = ByteBuffer.wrap(responseLengthBuffer);
            int msgLength = bb.getInt();

            byte[] responseRemBytes = new byte[msgLength];
            IOUtils.readFully(dis, responseRemBytes);
            long endTime = System.currentTimeMillis();

            byte[] fullResponseBytes = new byte[responseLengthBuffer.length + responseRemBytes.length];

            System.arraycopy(responseLengthBuffer, 0, fullResponseBytes, 0, responseLengthBuffer.length);
            System.arraycopy(responseRemBytes, 0, fullResponseBytes, responseLengthBuffer.length, responseRemBytes.length);
            LOG.debug("Response from server is received. Time elapsed at {} millis.", (endTime - startTime));
            LOG.debug("Bytes received: {}", Hexifier.toHex(fullResponseBytes));

            return fullResponseBytes;

        } else {
            reconnectToHost();
        }

    } catch (SocketTimeoutException  ste){
        LOG.error("Reading response from socket timeout: {}", ste.getClass().getName());

        try {
            LOG.warn("Waiting for Host reply from its socket timeout.");
            socket.shutdownInput();
            socket.shutdownOutput();

            int hostResponsefromTimeout = dis.read();
            if (hostResponsefromTimeout == -1){
                LOG.debug("Host has responded with -1");

            } else {
                LOG.debug("Host responded with: {}", hostResponsefromTimeout);
            }

        } catch (Exception e){
            LOG.error("Error encountered while trying to validate if Host responded with last timeout.");
           LOG.error(e.getMessage(), e);

        }


        reconnectToHost();

    } catch (EOFException eofe) {
        LOG.debug("An error occurred while reading stream from Host socket connection: {}", eofe.getClass().getName());
        LOG.error(eofe.getMessage(), eofe);

        if (eofe.getMessage() != null && eofe.getMessage().contains("actual: 0")){
            LOG.warn("No bytes were found at stream. Host did not respond.");
        }

        reconnectToHost();

    } catch (SocketException e) {
        LOG.error("An error occurred while attempting to connect to Host: {}", e.getClass().getName());
        LOG.error(e.getMessage(), e);

        if (e.getMessage().contains("Broken pipe")){
            LOG.debug("Connection to Host was lost.");
        }

        reconnectToHost();

    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        LOG.debug("An error occurred while attempting to connect to Host: {}", e.getClass().getName());
        reconnectToHost();
    }

    return null;
}

【问题讨论】:

  • 你有代码要分享吗?
  • 你好,请看我的代码

标签: java sockets ibm-midrange


【解决方案1】:

如果您在套接字上获得SocketTimeoutException,否则您打算将其重新用于另一个请求,请关闭它并且不要将其重新用于另一个请求。摆脱所有关闭并读取 catch 处理程序中的代码,然后关闭套接字。那么对于同一个套接字上的后续请求,您不可能得到“错误响应”。

如果您遇到很多超时,则说明您的超时太短了。通常,它应该是相关请求的预期服务时间的两倍。

【讨论】:

  • 您好,在 SocketTimeoutException 关闭套接字并为另一个请求创建一个新套接字之后的方法 reconnectToHost()。这是我最初的实施,但是仍然会遇到延迟响应。我添加了关闭的东西和读取的代码,以确保我优雅地断开了与主机的连接:[link] (stackoverflow.com/questions/2028620/…)
猜你喜欢
  • 2011-10-23
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多