The Runtime Theory

TCP Connection Lifecycle: SYN, ACK, Data, and the Four-Way Teardown

Follow a TCP connection through the three-way handshake, sequenced data exchange, and FIN-based teardown — with every kernel state from LISTEN to TIME-WAIT.

The Runtime Theory Team09 stages

trace / request.md

SERVER LISTENSCLIENT SENDS SYNSERVER ANSWERSSYN-ACKCLIENT ACKSSEQUENCEDDATA FLOWACTIVE CLOSE: FINPASSIVESIDE CLOSESTIME-WAITHOLDS ONBOTH SIDES CLOSED

readyThe server's socket is bound to an address and in LISTEN state; the kernel's accept queue is open for incoming SYN packets.

The lifecycle is a state machine living in the kernel. It starts with a passive open: the server binds and listens, and the accept queue stands ready. The handshake is three segments — SYN, SYN-ACK, ACK — during which both sides exchange initial sequence numbers, so the connection is established before a single byte of application data moves.

Once ESTABLISHED, data exchange is governed by sequence numbers, acknowledgments, and the advertised receive window; the sender may never have more unacknowledged bytes in flight than the window allows. Teardown is a four-way dance: FIN, ACK, then the other side's FIN and ACK. The active closer then pays the TIME-WAIT toll — 2×MSL — so stray segments from the dying connection cannot be mistaken for data on a reused connection. The ordering matters: neither side may reuse a sequence space or a port until the network has emptied of the connection's own packets.