1

对于很长的 POST 请求(超过 2 小时),我的以下测试代码运行良好:

        URL postURL = new URL(url);            
        con = (HttpURLConnection) postURL.openConnection();            
        con.setUseCaches(false);
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");            
        OutputStream out = con.getOutputStream();
        OutputStreamWriter wout = new OutputStreamWriter(out, "UTF-8");
        wout.write(xmlRequest);
        wout.flush();
        out.close();
        con.connect();            
        int httpResponseCode = HttpURLConnection.HTTP_SERVER_ERROR;
        try {
            httpResponseCode = con.getResponseCode();
        } catch (IOException e) {
            log(e.toString() + " Error retrieving the status code");
        }
        log("Failure status code " + httpResponseCode + " received: ");

我从一个主机到另一个主机运行这篇文章,它在所有环境中运行良好,除了一个确切的 linux redhat 主机 - 当我从这个主机运行这段代码时,我得到了异常:

java.net.SocketException: recv() failed, errno = 104 Connection reset by peer 检索状态码错误
收到失败状态码 500。

目标服务器主机在所有测试中都是同一主机。所以区别仅在于客户端调用者主机。

所以我试图了解这台 linux 机器上的确切 tcp 设置是什么导致接收在恰好 2 小时后失败。

我同意在这里为这种“不正确”使用发送帖子并等待超过 2 小时的回复而受到责备;)但问题是导致这种情况的原因

4

2 回答 2

0

google 7200 秒 tcp redhat 等等

   tcp_keepalive_time (integer; default: 7200; since Linux 2.2)
          The number of seconds a connection needs to be idle  before  TCP
          begins sending out keep-alive probes.  Keep-alives are only sent
          when the SO_KEEPALIVE socket option  is  enabled.   The  default
          value  is  7200 seconds (2 hours).  An idle connection is termi-
          nated after approximately an additional 11 minutes (9 probes  an
          interval of 75 seconds apart) when keep-alive is enabled.

          Note that underlying connection tracking mechanisms and applica-
          tion timeouts may be much shorter.
于 2012-06-24T19:37:40.620 回答
0

不同之处在于,一台客户端主机位于不同的网段,路由器在超时后终止空闲会话。

于 2013-04-17T16:43:10.160 回答