简体   繁体   English

如何防止对TCP套接字的延迟回复?

[英]How do I prevent late replies to a TCP socket?

I'm writing a database library that will have to intelligently support timeouts. 我正在编写一个必须智能支持超时的数据库库。 Now that I'm looking at connection pooling, I'm particularly worried about the following scenario: 现在我正在考虑连接池,我特别担心以下情况:

  1. Send Query #1 发送查询#1
  2. Time out after n seconds. n秒后超时。
  3. Send Query #2 发送查询#2
  4. Receive a response to query #1 收到对查询#1的响应

Step four can happen, because the queries are not marked with a query ID: all I know is I've received a response, and I don't know which query it belongs to. 第四步可能会发生,因为查询没有标记查询ID:我所知道的是我收到了一个响应,我不知道它属于哪个查询。 One could argue that this constitutes a mistake in the protocol, but that's not up to me. 有人可能会认为这在协议中构成了错误,但这不取决于我。

Before sending query #2, what should I do to the socket to avoid such a late reply? 在发送查询#2之前,我应该怎么做套接字以避免这么晚的回复? Is the only correct way to shutdown() , close() and re- connect() ? shutdown()close()和重新connect()的唯一正确方法吗?

I am afraid this is the only safe way to operate your connection, because there is no other way to get a one-to-one between question and answer over TCP. 我担心这是操作连接的唯一安全方式,因为没有其他方法可以通过TCP在问答之间进行一对一。 There seems to be a lack of some sort of a cancel function inbetween. 在它们之间似乎缺乏某种取消功能。

This reference gives more insight: http://www.ssfnet.org/Exchange/tcp/tcpTutorialNotes.html 此参考提供了更多见解: http//www.ssfnet.org/Exchange/tcp/tcpTutorialNotes.html

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

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