我可以在几个 readLine() 之后设置 HttpConnectionParams.setSoTimeout()

发布于 2024-09-29 20:11:44 字数 562 浏览 1 评论 0原文

我正在尝试编写一个基于JAVA的comet HTTP流。我为此使用 apache httpClient 4.x。由于它是 Comet HTTP 流,因此所有事件都以分块数据的形式到达客户端。由于连接可能会长时间保持打开状态,因此我尝试在来自服务器的一些 readLine() 数据后实现 SO_TIMEOUT 的动态设置。

当我在 BufferedInputStream 的 readLine 之后设置 SO_TIMEOUT 的值时,它没有生效,它仍然使用在execute()方法调用之前设置的原始 SO_TIMEOUT 值。

以下是 apache 网站上 SO_TIMEOUT 的定义: 定义套接字超时(SO_TIMEOUT),以毫秒为单位,这是等待数据的超时时间,或者换句话说,两个连续数据包之间不活动的最大时间段。超时值为零被解释为无限超时。

根据上述定义,SO_TIMEOUT 值应在每个分块数据到达时由 apache httpclient 重置。如果是这种情况,它应该接受我较新的 SO_TIMEOUT 值。关于如何在读取 BufferedInputStream 中的几行后设置 SO_TIMEOUT 值有什么想法吗?

I am trying to write a JAVA based comet HTTP streaming. I am using apache httpClient 4.x for this. Since, it is comet HTTP streaming, all the events are coming to the client in the form of chunked data. Since the connection may stay opended for long time, I am trying to implement the dynamic setting of SO_TIMEOUT after some readLine() data from the server.

When i set the value for SO_TIMEOUT after readLine from the BufferedInputStream, it is not taking effect, it is still using the original SO_TIMEOUT value set in before the execute() method call.

Following is the definition of SO_TIMEOUT from apache website:
Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). A timeout value of zero is interpreted as an infinite timeout.

As per the above definition, SO_TIMEOUT value should get reset by the apache httpclient on the arrival of every chunked data. If that is the case, it should accept my newer SO_TIMEOUT value. Any idea on how I can set the SO_TIMEOUT value after reading couple of lines form BufferedInputStream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐偏执 2024-10-06 20:11:44

根据上面的定义,SO_TIMEOUT 值应该在每个分块数据到达时由 apache httpclient 重置。

我认为您正在阅读引用的 javadoc 的含义,这并非作者的本意。我不读文本意味着您可以在读取响应流时动态调整超时。

事实上,如果您检查 setSoTimeout() 的代码并跟踪使用该参数的位置,很明显,仅在要创建 HTTP 方法时才使用 value。该时间之后所做的更改不会影响当前请求或响应的处理方式。查找HttpMethodDirector.applyConnectionParams

知道如何在读取 BufferedInputStream 中的几行后设置 SO_TIMEOUT 值吗?

我认为最好的选择是扩展 HttpConnection 类以添加一个更改连接套接字的 SO_Timeout 的方法。

As per the above definition, SO_TIMEOUT value should get reset by the apache httpclient on the arrival of every chunked data.

I think you are reading meaning into the quoted javadoc that was not intended by the authors. I don't read the text to mean that you can dynamically adjust the timeout while reading the response stream.

In fact, if you examine the code of setSoTimeout() and follow through to the places where the parameter is used, it is clear that value is only used when a HTTP method is about be made. Changes made after that time have no effect on the way that the current request or response is handled. Look for HttpMethodDirector.applyConnectionParams.

Any idea on how I can set the SO_TIMEOUT value after reading couple of lines form BufferedInputStream?

I think your best bet would be to extend the HttpConnection class to add a method that changes the SO_Timeout for the connection's socket.

梦明 2024-10-06 20:11:44

我相信,在 HttpConnection 对象上调用 open() 后,对 HttpConnectionParams 的任何更改都不会生效。查看 Javadocs 对于 HttpConnection。它说:

在 open() 方法中获取输入/输出流之前,在套接字上设置以下选项:

...
Socket.setSoTimeout(int) SO_TIMEOUT HttpConnectionParams.setSoTimeout(int)
...

I believe that after you call open() on the HttpConnection object, any changes to the HttpConnectionParams won't take affect. Take a look at the Javadocs for HttpConnection. It says:

The following options are set on the socket before getting the input/output streams in the open() method:

...
Socket.setSoTimeout(int) SO_TIMEOUT HttpConnectionParams.setSoTimeout(int)
...

楠木可依 2024-10-06 20:11:44

HttpConnection.setSocketTimeout。 “直接在底层套接字上设置 SO_TIMEOUT 值。”这将在底层套接字上的下一个阻塞读取时生效。当然,如果你有一个 BufferedInputStream 围绕它并且它已经有数据,那么你根本不会阻塞,直到你耗尽它。

HttpConnection.setSocketTimeout. 'Sets the SO_TIMEOUT value directly on the underlying socket.' That will take effect with the next blocking read on the underlying socket. Of course if you have a BufferedInputStream around that and it already has data in it you won't be blocking at all until you exhaust that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文