Compare TCP and UDP
Learn what transport protocols add above IP.
- Choose TCP or UDP based on delivery needs.
IP offers connectionless packet delivery and does not guarantee that packets arrive, arrive once, or arrive in order. TCP builds a reliable, ordered byte stream between endpoints. UDP provides datagrams with less transport machinery; applications choose how to handle loss or ordering. Both use port numbers to deliver data to processes.
1segments = [(2, "C"), (0, "A"), (1, "B")]
2ordered = sorted(segments)
3print("".join(payload for _, payload in ordered))
4print("TCP reorders bytes; UDP preserves datagram boundaries")ABC TCP reorders bytes; UDP preserves datagram boundaries
TCP does not preserve application message boundaries: it exposes a byte stream. UDP keeps datagram boundaries but does not promise delivery. Neither choice is automatically “faster”; the right protocol depends on the application’s needs.
Key takeaways
Choose TCP or UDP based on delivery needs.
The code examples simulate protocol behavior; they do not send network traffic.
Use the layer model to locate where an address, port, or message belongs.
Lesson quiz
4 questions · pass with 3 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.
Practice: model networking concepts
Use small Python programs to model packet, address, and protocol behavior. These exercises run locally in your browser and do not send network traffic.
Reassemble ordered segments
Sort the provided sequence-number and payload pairs, then print the payloads in order.
- Out-of-order arrival
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…