核心 · Key Idea
In one line: TCP sets up a logical connection between two hosts and delivers an upper-layer byte stream reliably and in order — retransmits losses, slows down on congestion. It underpins HTTP/SSH/SMTP/databases and most other protocols.
What it is#
TCP provides:
- Connection-oriented: 3-way handshake before data;
- Reliable delivery: sequence numbers + ACKs + retransmits;
- In-order: receiver reorders to send order;
- Flow control: sliding window, receiver tells sender "how much I can take";
- Congestion control: detect loss → slow down (Reno / CUBIC / BBR).
The cost is handshake / retransmit overhead — bad for real-time.
Analogy#
打个比方 · Analogy
TCP is a registered letter:
- Recipient signs (ACK);
- Lost in transit → courier resends (retransmit);
- Must arrive in order (sequence numbers);
- Recipient overloaded → tells you to slow down (sliding window).
Key concepts#
3-way handshake3-way handshake
SYN → SYN+ACK → ACK to establish a connection. See the TCP handshake page.
4-way close4-way close
FIN/ACK both directions to close.
SEQ / ACKSEQ / ACK
Byte counters; receiver tells sender 'next byte expected'.
Sliding windowSliding window
Receiver dynamically advertises 'still got X bytes' for flow control.
MSSMaximum Segment Size
Max payload bytes per segment, often = MTU - 40.
Congestion controlCongestion control
Reno / CUBIC / BBR pace by loss / RTT.
How it works#
The TCP header is at least 20 bytes — sequence, ack, window, checksum, flags (SYN / ACK / FIN / RST / PSH / URG).
Practical notes#
ss -tiprints current congestion algorithm, RTT, cwnd per connection.sysctl net.ipv4.tcp_congestion_controlchanges default. BBR usually outperforms CUBIC on transoceanic links.- Lots of TIME_WAIT: high-concurrency short-connection services accumulate them; tune
tcp_tw_reuseor use long connections / pooling. - Half-open / accept queue full: when a listener is overrun, SYNs get dropped; tune
somaxconnandtcp_max_syn_backlog. - TCP keepalive: 7200 s default is too long; long-connection services typically use 60–120 s.
Easy confusions#
TCP
Connection-oriented, reliable, ordered, congestion-controlled.
Handshake + retransmit overhead.
Handshake + retransmit overhead.
UDP
Connectionless, no guarantees, no order, no congestion control.
Zero overhead — fits real-time.
Zero overhead — fits real-time.