C++ – Is TCP message order still guaranteed when using async IO

cprotocolsocketstcp

Here the scenario I'm imagining. Two messages are transferred, both will require 2 packets each (I know you don't deal with packets directly with TCP, but on the IP level each message will consist of 2 packets [or segments as they're called in TCP])

The problem I'm imaging is buffering the first packet of the first message, but a router dumps the second packet for some reason. Being TCP, the second packet is guaranteed to be resent, and I know that TCP will hand these two packets up to the application layer in order. But while being handled asynchronously, this first packet will still be handed up to the application which will not block during the period in which the underlying TCP layer handles the retransmission of the second packet.
What will happen if the first packet of the second transmission arrives before the last packet of the first transmission? Will TCP not hand these packets up until the first transmission is handled in full? Will an async client not send the second message before the first has been handled in full?

I know that TCP will guarantee the order of packets for each message. My confusion is whether it will fully complete one transmission before processing the second transmission when being handled asynchronously.

Best Answer

TCP is a stream protocol (not packets as you clearly said). The TCP stack layer will only deliver the data in stream order. Async or not, the data will not be handed to the client out of order.

TCP would not be a very good streaming protocol if it did deliver data out of order. That's kind of the point of the protocol. :-)

Related Topic