Understanding TCP vs UDP: Differences, Port Numbers, and Practical Uses
This article explains the core characteristics of TCP and UDP, compares their reliability and connection models, clarifies the concept and range of port numbers, and demonstrates that both protocols can share the same port without conflict, illustrated with real‑world examples such as DNS.
Today I saw a ByteDance interview question on Zhihu and decided to share it.
1. TCP Protocol
TCP (Transmission Control Protocol) is a connection‑oriented, reliable, byte‑stream transport layer protocol. It is one of the most widely used protocols in the Internet protocol suite. Its main features include:
Connection‑oriented : TCP establishes a connection between the sender and receiver before data transfer, commonly known as the three‑way handshake.
Reliability : TCP guarantees the order and integrity of data packets and requests retransmission if loss or corruption occurs.
Flow and congestion control : TCP can control the transmission rate to avoid network overload.
Bidirectional communication : Once a connection is established, data can flow in both directions.
2. UDP Protocol
UDP (User Datagram Protocol) is a simple, connection‑less transport layer protocol. Compared with TCP, UDP has different characteristics:
Connection‑less : UDP does not establish a connection before sending data, allowing immediate transmission.
No reliability guarantee : UDP does not ensure packet order, completeness, or uniqueness.
Lightweight : UDP has a small header overhead and fast processing, suitable for real‑time applications such as video streaming and online gaming.
No flow or congestion control : UDP does not adjust the sending rate, which may lead to packet loss under network congestion.
3. Port Numbers
After understanding TCP and UDP, let's look at port numbers.
In computer networks, a port is an abstract communication endpoint used to differentiate processes or network services. A port number is a 16‑bit integer ranging from 0 to 65535.
Ports allow a single computer to run multiple network applications simultaneously, such as a web browser, email client, and chat software, by distinguishing traffic for each application.
Think of an IP address as a house address and a port number as a specific room number within that house.
4. Can TCP and UDP Use the Same Port Number?
Yes, they can. Although TCP and UDP share the same port number space, each protocol manages its ports independently. This means a TCP service can listen on port 80 for HTTP, while a UDP service can also use port 80 for a different purpose without conflict.
The operating system and network devices differentiate traffic based on both the protocol type (TCP or UDP) and the port number. For example, DNS uses port 53 for both TCP (for large queries or zone transfers) and UDP (for standard queries).
This independent management allows flexible network communication and efficient sharing of network resources.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.