Understanding TCP: ACKs, Retransmission, Three‑Way Handshake & Four‑Way Termination
This article explains TCP's core mechanisms—including acknowledgment (ACK), timeout retransmission, the three‑way handshake for establishing connections, and the four‑way handshake for graceful termination—while illustrating each concept with diagrams and addressing common interview questions.
TCP Characteristics
1. Acknowledgment mechanism (ACK)
2. Timeout retransmission
3. Connection establishment – three‑way handshake
4. Connection termination – four‑way handshake
1. Acknowledgment mechanism (ACK)
ACK is the core of reliable transmission; the receiver sends an ACK packet to confirm receipt of data.
When messages are sent, they may arrive out of order ("post‑send‑first‑arrival"). Numbering each byte allows the receiver to acknowledge specific sequence numbers without excessive bandwidth usage.
Each byte in TCP has a sequence number; the ACK field set to 1 indicates an acknowledgment packet, and the "acknowledgment number" field carries the next expected byte.
Initial sequence numbers are random to prevent attacks; every transmitted segment carries its own sequence number, allowing the receiver to inform the sender which bytes have been received.
2. Timeout retransmission
If a packet or its ACK is lost, the sender cannot determine the cause, so TCP starts a timer after sending data; if the timer expires without receiving an ACK, the segment is retransmitted (timeout retransmission).
When the same data is retransmitted, TCP deduplicates based on sequence numbers to ensure the application receives each byte only once.
3. Connection establishment – three‑way handshake
The handshake verifies both ends can send and receive, and negotiates parameters such as the initial sequence number.
Illustrated with a phone‑call analogy, the three steps are:
Client sends SYN (SEQ=x).
Server replies with SYN+ACK (ack=x+1, SEQ=y).
Client sends ACK (ack=y+1), and both enter ESTABLISHED state.
Key TCP states during this process include LISTEN, SYN_SENT/SYN_RECV, and ESTABLISHED.
4. Connection termination – four‑way handshake
Termination also uses a handshake to ensure all data is transmitted before closing.
Client sends FIN,ACK (SEQ=u) and enters FIN_WAIT_1.
Server acknowledges with ACK (SEQ=v) and enters CLOSE_WAIT; client moves to FIN_WAIT_2.
Server sends its own FIN,ACK (SEQ=w) after all data is sent, entering LAST_ACK.
Client acknowledges with ACK (SEQ=u+1), entering TIME_WAIT, then after 2 MSL the connection is fully closed.
TIME_WAIT ensures delayed packets are handled and prevents old duplicate segments from affecting new connections.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
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.