7

I'm developing C++ application server and client which use TCP. I have three messages on server: A, B and C. They are sent sequentially: A -> B -> C. And clients responses acknowledge messages:rA, rB, rC.

Do client receive A, B and C in order A->B-C? Do server receive rA->rB->rC?

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
user469786
  • 71
  • 1
  • 2

1 Answers1

9

TCP guarantees that the order the packets are received (on a single connection) is the same as the order they were sent. No such guarantee if you've got multiple TCP connections, though - TCP preserves ordering only for the packets within a given TCP connection.

See the Wikipedia article on TCP for more overview.

One of the functions of TCP is to prevent the out-of-order delivery of data, either by reassembling packets into order or forcing retries of out-of-order packets.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 1
    just to clarify "no such guarantee if you've got multiple tcp connections": TCP guarantees order over a particular connection, independently of all other connections. that means if you send A over the first connection and B over the second connection, they could be delivered in order or out of order. If you send them over the same connection, they'll be delivered in order. – atk Oct 08 '10 at 02:56
  • Right. In particular, if you're creating separate connections for each message (as opposed to sending them sequentially over a single connection), they could arrive in any order. – David Gelhar Oct 08 '10 at 02:58
  • 3
    Saying "TCP guarantees the order of the packets" is slightly misleading. Remember, TCP provides a stream of bytes, not packets. – Robᵩ Mar 10 '11 at 00:24
  • packets may go through different routers, so network delay is posible. – TAKCHI CHAN Apr 02 '21 at 04:16