简体   繁体   中英

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
  2. Time out after n seconds.
  3. Send Query #2
  4. Receive a response to query #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. 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? Is the only correct way to shutdown() , close() and re- 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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