SkillGridLearn computer science by doing

TCP and UDP: reliable vs fast

How TCP guarantees delivery, why UDP does not, and when each is the right tool.

12 min · 40 XP

IP delivers packets on a best-effort basis: they can be lost, duplicated, or arrive out of order. Applications rarely want that. TCP adds reliability on top of IP, while UDP deliberately leaves it out.

TCP three-way handshake
ClientCLOSED
ServerCLOSED

Connection closed. Nothing is connected yet.

Packets sent
0
Retransmits
0
Status
Not yet established

Before any data flows, TCP performs a three-way handshake: the client sends SYN, the server replies SYN-ACK, and the client confirms with ACK. Only after that do both sides know the connection is live.

UDP skips the handshake entirely. It is faster and has less overhead, which is why live video, games, and DNS use it. Loss of a single packet is acceptable; waiting for a retransmit is not.

ClientServerSYN (seq=x)SYN-ACK (seq=y, ack=x+1)ACK (ack=y+1)Соединение установлено

Practice

  1. Put the three-way handshake steps in the correct order.

    Hints
    • Start with whoever initiates the connection.
    • An ACK always answers the previous step.

    10 XP

  2. A video call drops one frame. Which protocol is more appropriate, and why?

    Hints
    • Think about what happens when a packet is lost.
    • TCP trades speed for reliability.

    12 XP

  3. The first SYN is lost. Complete the handshake by retrying correctly.

    Hints
    • The timer expired without a reply.
    • The client resends SYN with the same sequence number.

    18 XP