TCP and UDP
Learn the fundamental differences between TCP and UDP, two core internet protocols. Understand when to use reliable, ordered TCP versus fast, connectionless UDP
In depth
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two foundational protocols that enable communication over the internet, each designed for different priorities. Understanding their distinctions is crucial for building robust and efficient network applications.
IP and Basic Packet Delivery
At the lowest level, IP (Internet Protocol) sends individual packets of data across a network. IP itself offers no guarantees: packets can be lost, duplicated, or arrive out of order. TCP and UDP build upon IP to add different levels of service.
TCP: Reliable, Ordered Delivery
TCP provides a reliable, ordered, and error-checked stream of data. It achieves this through several mechanisms:
Three-Way Handshake
Before any data is exchanged, TCP establishes a connection using a three-way handshake: a SYN (synchronize) packet from the client, a SYN-ACK (synchronize-acknowledge) from the server, and a final ACK (acknowledge) from the client. This sets up the communication parameters.
Packet Numbering and Acknowledgment
TCP assigns a sequence number to each packet. The receiver acknowledges successful receipt of packets. If a packet is lost or arrives out of order, the receiver detects the gap. The sender then retransmits the missing packet until it is acknowledged, ensuring all data arrives correctly and in the intended sequence.
Flow and Congestion Control
TCP dynamically adjusts the data transmission rate based on the network's capacity and the receiver's ability to process data. This prevents a fast sender from overwhelming a slow receiver or causing network congestion, optimizing overall throughput.
UDP: Fast, Connectionless Communication
UDP, in contrast, is a connectionless and minimal protocol. It offers a "fire-and-forget" approach to data transmission.
No Handshake, No Guarantees
UDP does not establish a connection with a handshake. It simply sends datagrams (packets) to a destination. There are no mechanisms for retransmission, ordering, or flow control. Packets may be lost, duplicated, or arrive out of sequence without any notification to the sender or receiver.
Core UDP Steps
Sender:
Create UDP socket
Bind to local address (optional)
Send data to destination address
Receiver:
Create UDP socket
Bind to local address
Listen for incoming data
Process received data (if any)Choosing Between TCP and UDP
TCP Prioritizes Reliability
TCP is ideal for applications where data integrity and complete, ordered delivery are paramount. Examples include web browsing (HTTP), email (SMTP), and file transfers (FTP).
UDP Prioritizes Speed and Low Latency
UDP is preferred when speed and low latency are critical, and occasional data loss is acceptable. This makes it suitable for real-time applications like online gaming, live video streaming, and voice over IP (VoIP), where retransmitting old data would be counterproductive.
Key Takeaways
- TCP is connection-oriented: It establishes a connection before data transfer.
- TCP guarantees delivery: It uses acknowledgments and retransmissions for reliability.
- TCP ensures order: Packets arrive in the correct sequence.
- UDP is connectionless: It sends data without prior setup.
- UDP is fast but unreliable: It offers no guarantees for delivery or order.
Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.
Ask your own question →