Differences and Principles of TCP and UDP Protocols
This article explains the fundamental differences between TCP and UDP, covering their purposes, reliability, speed, handshake mechanisms, data transmission processes, and termination procedures, and includes visual diagrams to illustrate each concept in.
TCP and UDP Protocol Differences and Principles
Recently I revisited the principles and differences of TCP and UDP and made a concise summary.
1. Purpose
Both TCP and UDP operate at the transport layer to transmit data between programs, such as files, video, JPG images, etc.
2. Differences
TCP is connection‑oriented, while UDP is connection‑less.
TCP provides reliable, stable data transmission , suitable for scenarios that require high communication quality, such as file transfer, email, web browsing, and so on.
UDP offers high speed but may lose packets , making it suitable for real‑time applications that can tolerate occasional loss, such as DNS queries, voice calls, video live streaming, and tunneling networks like VXLAN.
As an analogy, UDP is like sending a letter without knowing whether it arrives or is complete, whereas TCP is like a phone call where feedback is immediate and delivery is guaranteed.
3. TCP Communication Process
How does TCP ensure reliable transmission?
It consists of three steps: three‑way handshake, data transmission acknowledgment, and four‑way termination. The three‑way handshake establishes the connection.
4. Three‑Way Handshake
When a client initiates a connection, it first sends a SYN packet to ask whether a connection can be established. If the server agrees, it replies with a SYN‑ACK packet; the client then sends an ACK packet, completing the three‑way handshake.
Why three‑way instead of two‑way?
This prevents problems caused by delayed or duplicated SYN packets that could lead to inconsistent connection states.
If only two‑way handshake were used and the first SYN packet were lost but later retransmitted, the server might treat the retransmitted SYN as a new connection, causing the client to think a single connection exists while the server sees two, resulting in state mismatch.
Thus, the three‑way handshake solves the unreliability of the network channel, ensuring both sides agree on the connection state before data transfer.
5. Data Transmission
Data Transmission:
A single data unit may be split into multiple packets; TCP handles loss, out‑of‑order delivery, and duplication by using sequence numbers, acknowledgments, and a send buffer.
Each connection starts with a sequence number of 0; every byte increments the sequence number. When sending, TCP attaches the sequence number and length to each segment. The receiver acknowledges with an ACK equal to the received sequence number plus length, confirming receipt. This request‑response model ensures reliable delivery while allowing the sender to transmit multiple packets consecutively.
6. Four‑Way Termination
Either side can initiate connection closure. The client sends a FIN packet, enters FIN‑WAIT‑1, the server replies with ACK (entering CLOSE‑WAIT), then the server sends its own FIN, which the client acknowledges. This four‑step exchange ensures both sides have reliably received the termination signals even on unreliable networks.
The client waits for a timeout after sending the final ACK to guarantee the server has received it; otherwise the server will retransmit its FIN, and the client will resend the ACK, similar to the reliability mechanisms in the three‑way handshake.
7. UDP Protocol
UDP is connection‑less; it simply encapsulates data into packets and sends them without establishing state, resulting in minimal overhead and low CPU/memory usage compared to TCP.
However, because UDP does not guarantee delivery, it is less reliable than TCP. Its main advantages are speed and low resource consumption, making it suitable for real‑time applications that can tolerate occasional packet loss.
In summary, the primary differences are:
TCP provides reliable, stable transmission for high‑quality communication scenarios (file transfer, email, web browsing, etc.).
UDP offers fast transmission but may lose packets, fitting real‑time or low‑latency scenarios such as DNS queries, voice calls, video streaming, and tunneling protocols like VXLAN.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.