Compare TCP and UDP
Learn what transport protocols add above IP.
- Explain how ports deliver data to the right program.
- Trace the TCP three-way handshake.
- Choose TCP or UDP based on delivery needs.
IP gets a packet to a machine, on a best-effort basis. It makes no promises: packets can be lost, duplicated or arrive out of order, and it has no idea which program should get them. The transport layer deals with those two problems, and the two main protocols deal with them very differently.
Ports: from a machine to a program
A port is a 16-bit number that identifies a program's endpoint. Servers listen on well-known ports, such as 443 for HTTPS and 53 for DNS. Clients get a temporary port from the operating system.
A TCP connection is identified by four values: source address, source port, destination address and destination port. That's how a server handles thousands of connections on port 443 without confusing them.
TCP: a reliable conversation
TCP turns unreliable packets into a reliable, ordered byte stream. It numbers every byte, and the receiver acknowledges what has arrived, so lost data can be sent again and out-of-order data can be rearranged.
Before any of that, both sides have to agree on starting numbers. That's the three-way handshake.
Try it
Up to 15 XPOpen a TCP connection
Follow the sequence numbers. Each side picks a random starting number, and an acknowledgement says “the next number I expect”. Predict before you reveal.
Here is that same conversation in a real capture. [S] is SYN, [S.] is SYN-ACK, [.] is a plain ACK and [P.] is data being pushed to the application.
1$ sudo tcpdump -n -S host 203.0.113.8
2IP 192.168.1.10.51514 > 203.0.113.8.443: Flags [S], seq 1000, length 0
3IP 203.0.113.8.443 > 192.168.1.10.51514: Flags [S.], seq 5000, ack 1001, length 0
4IP 192.168.1.10.51514 > 203.0.113.8.443: Flags [.], ack 5001, length 0
5IP 192.168.1.10.51514 > 203.0.113.8.443: Flags [P.], seq 1001:1501, ack 5001, length 500
6IP 203.0.113.8.443 > 192.168.1.10.51514: Flags [.], ack 1501, length 0UDP: just send it
UDP adds ports and a checksum to IP, and almost nothing else. There's no handshake, no acknowledgement and no retransmission. Each datagram stands alone: it arrives whole or not at all.
That sounds worse, but it's the right choice when old data is useless. In a video call, a packet retransmitted half a second later would arrive after that moment has passed, so it's better to skip it and move on.
Try it
Up to 15 XPPick the transport
For each application, ask: is late data worse than missing data? If every byte must arrive, you probably want TCP.
“Downloading a software update”
“A live voice call”
“Loading a bank statement”
“A DNS lookup”
“A fast multiplayer game's position updates”
“Sending an email”
Try it
Up to 15 XPRead connections and captures
ss lists a machine's sockets (netstat is the older equivalent). Click the line that answers each question.
Which line shows the web server waiting for new HTTPS connections?
Key takeaways
Ports direct data to a program, and four values identify a TCP connection.
TCP gives a reliable, ordered byte stream, at the cost of a handshake.
UDP sends independent datagrams and leaves reliability to the application.
Lesson quiz
7 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…