Communication
TCP 与 UDP
传输层协议对比
Trade-off 快览
- TCP:可靠、有序、重传,适合对完整性要求高的场景
- UDP:更快、低开销,但可能丢包,适合实时与低 latency 场景
示例:
- email / 文件传输 → TCP
- 视频直播 / online gaming → UDP
TCP
Transmission Control Protocol (TCP) 是 connection-oriented 的协议,一旦 connection 建立,数据就可以双向传输。TCP 自带 error-checking 和顺序保证,能保证数据按发送顺序到达,适合传输静态图片、data files、web pages 等内容。

TCP 很可靠,但它的反馈机制会带来更大的 overhead,占用更多 network 带宽。
UDP
User Datagram Protocol (UDP) 是更简单的 connectionless 协议,不要求 error-checking 与 recovery。UDP 不需要建立/维护/终止 connection 的 overhead,数据会持续发送给接收方,不管对方是否真的收到。

UDP 常用于 real-time 通信,比如 broadcast / multicast。
当你更在意 最低 latency,且“晚到的数据比丢数据更糟”时,应优先用 UDP。
TCP vs UDP
TCP 是 connection-oriented;UDP 是 connectionless。
关键差异之一是 speed:TCP 相对更慢。总体而言 UDP 更快、更简单、更高效,但丢包重传只在 TCP 里可用。
TCP 提供 ordered delivery(user ↔ server),而 UDP 不保证 end-to-end 顺序,也不会检查 receiver readiness。
| Feature | TCP | UDP |
|---|---|---|
| Connection | Requires an established connection | Connectionless protocol |
| Guaranteed delivery | Can guarantee delivery of data | Cannot guarantee delivery of data |
| Re-transmission | Re-transmission of lost packets is possible | No re-transmission of lost packets |
| Speed | Slower than UDP | Faster than TCP |
| Broadcasting | Does not support broadcasting | Supports broadcasting |
| Use cases | HTTPS, HTTP, SMTP, POP, FTP, etc | Video streaming, DNS, VoIP, etc |