IT and software

Opening and closing a TCP connection (eleven states)

Opening a TCP connection takes three segments and closing it takes four, and the extra one is the whole point: "I have nothing more to send" and "I will not receive any more" are separate statements, so between the two FINs one side is still allowed to send. The eleven RFC 793 states are listed beside the ladder, along with why the last side to send an ACK has to wait before it disappears.

Opening a TCP connection takes three segments and closing it takes four, and the extra one is the whole point: "I have nothing more to send" and "I will not receive any more" are separate statements, so between the two FINs one side is still allowed to send. The eleven RFC 793 states are listed beside the ladder, along with why the last side to send an ACK has to wait before it disappears.

This drawing comes from one YAML file. No shape was moved by hand.

Notes in the source (in Japanese)

つなぐのは 3 通、切るのは 4 通。 その 1 通の差が、この図の中身。

クライアントが SYN(乱数 A を seq に)を送り、サーバが SYN + ACK(seq に乱数 B、ack に A + 1)を返し、クライアントが ACK(seq に A + 1、ack に B + 1)を返す。3 通。

FIN → ACK → FIN → ACK。 なぜ 4 通かというと、「もう送らない」と「もう受け取らない」が別の話だから。片方が FIN を送ってから相手が FIN を送るまでの間、送り終えたのは片方だけという状態(半クローズ)があり、相手はまだ送れる。 その区間が CLOSE-WAIT と FIN-WAIT-2。

最後の ACK を送った側は、すぐには消えない。最後の ACK が落ちたら、相手は FIN を送り直してくる ——そのとき応えられる者がいないと、相手は待ち続ける。だから 2MSL(RFC 9293 の MSL = 最大セグメント生存時間は 2 分。合わせて 4 分)待つ。実装では 30 秒・1 分・2 分が多い。

CLOSING は、両側が同時に FIN を送ったときだけ通る道なので、この梯子には現れない。

クライアントが ESTABLISHED になるのは、SYN + ACK を受けた時点(最後の ACK を送る前)。サーバは最後の ACK を受けて ESTABLISHED になる。RFC 9293 の Figure 6 がこの順で書いている。

ヘッダのビットの並びは見本 234。状態の説明は RFC 793 の語をそのまま使った。RFC 793 は 2022 年に RFC 9293 で置き換えられた(状態の名前・11 個・MSL 2 分は同じ)。出典:https://www.rfc-editor.org/rfc/rfc9293.html (2026-09-29 取得)

Source (YAML)

examples/gallery/320-TCPのつなぎ方と切り方.zumen.yaml

More in this field