Unit 2: The Internet · Transporting packets

Article

TCP, the reliable way

6 min read

You download a program to install. If even one small piece is missing, the program may not run at all. Here a glitch is not acceptable. You want every byte, correct and in order. A protocol called TCP is built for exactly that.

TCP: make sure it all arrives

TCP (Transmission Control Protocol) is a protocol that makes the sending of packets reliable. It works together with IP: IP still addresses and routes the packets, and TCP adds the checking. TCP does these things:

  1. It sets up a connection first. The two computers agree to talk before data is sent.
  2. It numbers the packets, so the receiver knows which piece is which.
  3. The receiver sends back an acknowledgement. An acknowledgement is a short message that says "I got packet 4".
  4. The sender resends packets that are not acknowledged. If no acknowledgement comes back after a short wait, the sender assumes the packet was lost and sends it again.
  5. The receiver puts the packets in order, throws away any copy it already has, and checks that the data was not damaged.

Here is a lost packet being repaired:

TCP repairs a lost packet

  1. 1

    Sender → Receiver

    Can we talk?

  2. 2

    Receiver → Sender

    Yes, ready.

  3. 3

    Sender → Receiver

    packet 1

  4. 4

    Receiver → Sender

    got 1

  5. 5

    Sender → Receiver✕ lost

    packet 2

  6. No acknowledgement for packet 2 comes back, so the sender waits and then sends it again.
  7. 6

    Sender → Receiver

    packet 2 again

  8. 7

    Receiver → Sender

    got 2

Dashed arrows are the receiver's replies. The missing reply is how the sender finds out that something was lost.

The sender never knew why packet 2 vanished. It only noticed that the acknowledgement did not come, and that was enough.

Check your understanding

A sender uses TCP. It sends packet 7, but no acknowledgement for packet 7 comes back. What does TCP do?

The price of reliability

All this checking takes time. TCP has to set up the connection, wait for acknowledgements, and sometimes wait for a lost packet to be sent again. So TCP is usually slower than UDP and can have more delay. Here are the two protocols side by side:

UDPTCP
Sets up a connection firstNoYes
AcknowledgementsNoYes
Resends lost packetsNoYes
Puts packets in orderNoYes
Speed and delayFaster, lower delaySlower, more delay
Good forLive calls, gamesFiles, programs, web pages, email

Use TCP when every bit must arrive correctly and a short wait is acceptable. Use UDP when a late packet is worth nothing.

Check your understanding

Why is TCP usually slower than UDP?

Worked example

A 1,000-packet file is sent with TCP. On the first try, 3 packets are lost.

  1. The receiver collects 997 packets and sends acknowledgements for them.
  2. For the 3 missing packets, no acknowledgements go back.
  3. After the wait, the sender sends those 3 packets again. This time they arrive.
  4. The receiver now has all 1,000, sorts them by number, and the file is complete.

The download took a little longer because of the 3 resends, but the file is perfect. With UDP, the same file would have 3 holes, and a program with holes would probably not work.

Common mistakes

  • "TCP stops packets from being lost." No. Packets can still be lost on the way. TCP notices and sends them again.
  • "TCP makes all packets take the same route." No. Packets can still take different routes. TCP puts them in order at the end.
  • "TCP is always better than UDP." No. For a live call, TCP's waiting would make the call lag.
  • "An acknowledgement carries the data." No. It is only a short receipt that says what arrived.

On the exam

TCP means reliable delivery: numbering, acknowledgements, resending and reordering, at the cost of some speed. If a question needs complete and correct data, such as a download or an email with an attachment, pick TCP. If it needs low delay, pick UDP.

Key words

TCP (Transmission Control Protocol): a protocol that makes delivery reliable by numbering packets, checking for acknowledgements and resending lost packets.

Acknowledgement: a short message from the receiver that says a packet arrived.

Reliable: sure to deliver all the data, correctly and in order.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.