java - java 套接字中收到的延迟响应

标签 java sockets ibm-midrange

我需要有关 as400 和 java 连接的帮助。

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

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

下面是我连接到 as400 服务器的代码片段:

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;
}

最佳答案

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

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

关于java - java 套接字中收到的延迟响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21118370/

相关文章:

c - 如何绕过阻止直接连接的网站?

java - 按键获取GAE实体

python - 如何将终端窗口聊天应用程序(使用套接字编程构建,用 python 编写)转换为 Web 应用程序?

c - 通过 C 套接字发送原始二进制数据?

ibm-midrange - AS400 Telnet 模拟器自动登录

ibm-midrange - RPGLE/free - 动态指定数据区库

java - 如何进行数据库查询的 JUNIT 测试

java - JAXB - 从 XSD 生成类 - 将枚举转换为自定义 toString() 而不是枚举名称

使用byte []的java biginteger加法

sql - 查询定义字段的设置格式