简体   繁体   English

我怎么知道什么时候关闭HTTP 1.1 Keep-Alive连接?

[英]How do I know WHEN to close an HTTP 1.1 Keep-Alive Connection?

I am writing a web server in Java and I want it to support HTTP 1.1 Keep-Alive connections. 我正在用Java编写Web服务器,我希望它支持HTTP 1.1 Keep-Alive连接。 But how can I tell when the client is done sending requests for a given connection? 但是,如何判断客户端何时发送给定连接的请求? (like a double end-of-line or something). (如双线或类似物)。

Lets see how stackoverflow handles this very obscure question -- answers for which, on Google, are mired in technical specifications and obscure language. 让我们看看stackoverflow如何处理这个非常模糊的问题 - 谷歌在技术规范和模糊语言中陷入困境的答案。 I want a plain-english answer for a non-C programmer :) 我想要一个非C程序员的简明英语答案:)


I see. 我知道了。 that confirms my suspicion of having to rely on the SocketTimeoutException. 这证实了我怀疑不得不依赖于SocketTimeoutException。 But i wasn't sure if there was something i could rely on from the client that indicates it is done with the connection--which would allow me to close the connections sooner in most cases--instead of waiting for the timeout. 但是我不确定我是否可以从客户端依赖它来表明它已完成连接 - 这将允许我在大多数情况下更快地关闭连接 - 而不是等待超时。 Thanks 谢谢

If you're building your server to meet the standard, then you've got a lot of information to guide you here already. 如果您正在构建满足标准的服务器,那么您已经获得了很多信息来指导您。

Simple spoken, it should be based on a time since a connection was used, and not so much at the level of request data. 简单说一下它应该基于自使用连接以来的时间,而不是基于请求数据的级别。

In a longer-winded way, the practical considerations section of the HTTP/1.1 document has some guidance for you: 以较长的方式,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." “服务器通常会有一些超时值,超出这些值后,它们将不再维持非活动连接。代理服务器可能会使此值更高,因为客户端可能会通过同一服务器建立更多连接。持久性的使用连接对客户端或服务器的超时长度(或存在)没有要求。“

or 要么

"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. 让我们看看stackoverflow如何处理这个非常模糊的问题 - 谷歌在技术规范和模糊语言中陷入困境的答案。

I just put When should I close an HTTP 1.1 connection? 我刚才应该何时关闭HTTP 1.1连接? into Google, and the third hit was HTTP Made Really Easy. 进入谷歌,第三个打击是HTTP Made Really Easy。 In the table of contents, there is a link to a section entitled Persistent Connections and the "Connection: close" Header . 在目录中,有一个指向标题为持久连接的部分和“连接:关闭”标题的链接 This section is three paragraphs long, uses very simple language, and tells you exactly what you want to know. 这部分是三段长,使用非常简单的语言,并准确地告诉你你想知道什么。

I want a plain-english answer for a non-C programmer :) 我想要一个非C程序员的简明英语答案:)

With all due respect, programming is a technical endeavour where the details matter a great deal. 在充分尊重的情况下,编程是一项技术性工作,其中细节非常重要。 Reading technical documentation is an absolutely essential skill. 阅读技术文档是绝对必要的技能。 Relying on "plain English" third-party interpretations of the specifications will only result in you doing a poor job. 依靠“普通英语”第三方对规范的解释只会导致你做得不好。

You close it whenever you'd like. 你可以随时关闭它。 The header indicates that the client would prefer you to leave the connection open, but that doesn't require the server to comply. 标头表示客户端希望您保持连接打开,但这不需要服务器遵守。 Most servers leave it open for about 5-10 seconds, some don't pay attention to it at all. 大多数服务器将其打开大约5-10秒,有些服务器根本不关注它。

You should read the RFCs dealing with the Keep-Alive feature. 您应该阅读处理Keep-Alive功能的RFC。 Otherwise you might end up with a server that doesn't work as expected. 否则,您可能最终得到的服务器无法按预期工作。

As @[Stephen] has already pointed out, the server is free to close the connection anytime it wishes (ok, not in the middle of a request/response pair though). 正如@ [Stephen]已经指出的那样,服务器可以随时随地关闭连接(好吧,不是在请求/响应对的中间)。 Ditto for the client. 同样适合客户。 Any other solution would allow the server or the client to perform a DoS on the other party. 任何其他解决方案都允许服务器或客户端在另一方上执行DoS。

EDIT: Have a look at the Connection header. 编辑:看看Connection标题。 The client (and the server) can request a graceful connection closure using the header. 客户端(和服务器)可以使用标头请求正常的连接关闭。 For example, Connection: close inside the request is a request to the server to close the connection after it sends the response. 例如, Connection: close请求是服务器在发送响应后关闭连接的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM