我怎么知道何时关闭HTTP 1.1保持活动连接?
我正在用Java编写Web服务器,并且希望它支持HTTP 1.1 Keep-Alive连接。但是,如何确定客户端何时完成针对给定连接的请求发送呢? (例如,双行结束符)。
让我们看看stackoverflow如何处理这个非常晦涩的问题-在Google上,其答案被技术规范和晦涩的语言所困扰。我想要一个非C程序员的简单英语答案:)
我知道了。这证实了我对必须依赖SocketTimeoutException的怀疑。但是我不确定客户端是否可以依靠某些内容表明连接已完成-在大多数情况下,这将允许我尽快关闭连接-而不是等待超时。谢谢
解决方案
我们可以随时关闭它。标头指示客户端希望我们保持打开状态,但这不需要服务器遵守。大多数服务器将其保持打开状态约5至10秒钟,有些服务器根本不关注它。
我们应该阅读有关"保持活动"功能的RFC。否则,我们最终可能会无法正常使用服务器。
正如@ [Stephen]已经指出的那样,服务器可以随时随地关闭连接(可以,尽管不在请求/响应对中间)。与客户同上。任何其他解决方案都将允许服务器或者客户端对另一方执行DoS。
编辑:看看连接头。客户端(和服务器)可以使用标头请求正常关闭连接。例如,请求内部的" Connection:close"是在服务器发送响应后向服务器发出的关闭连接的请求。
如果我们要构建满足标准的服务器,那么我们已经有很多信息可以指导我们。
简单地说,它应该基于使用连接以来的时间,而不是基于请求数据级别。
从长远来看,HTTP / 1.1文档的"实际考虑因素"部分为我们提供了一些指导:
"Servers will usually have some time-out value beyond which they will no longer maintain an inactive connection. Proxy servers might make this a higher value since it is likely that the client will be making more connections through the same server. The use of persistent connections places no requirements on the length (or existence) of this time-out for either the client or the server."
或者
"When a client or server wishes to time-out it SHOULD issue a graceful close on the transport connection. Clients and servers SHOULD both constantly watch for the other side of the transport close, and respond to it as appropriate. If a client or server does not detect the other side's close promptly it could cause unnecessary resource drain on the network."
Lets see how stackoverflow handles this very obscure question -- answers for which, on Google, are mired in technical specifications and obscure language.
我刚刚输入了何时应该关闭HTTP 1.1连接?进入Google,第三个热门是HTTP Made Really Easy。在目录中,有一个指向"持久连接"部分和"连接:关闭"标题的链接。本节长三段,使用非常简单的语言,并准确告诉我们我们想知道什么。
I want a plain-english answer for a non-C programmer :)
出于所有应有的尊重,编程是一项技术努力,其中细节至关重要。阅读技术文档是绝对必要的技能。依靠规范的"普通英语"第三方解释只会导致我们做得很差。