how/network
Three-way handshake, then data, then four-way close. Why three handshakes, and why `ss` shows so many states after a connection closes.
TCP is a conversation with acknowledgments. Before they start exchanging data, the two sides have to "agree": each one picks its own random "starting number", and each one has to confirm that it saw the other's. This takes three packets, and that is the famous three-way handshake.
After the handshake, data flows freely. When the sides want to finish, they "say goodbye" separately in both directions (TCP is half-duplex on close: one side can say "that's all from me", while the other still has something to send).
Out of all these transitions a state machine is born: on each
side the connection passes through a chain of states. You can see them
live with ss -tn, and every ESTABLISHED/TIME_WAIT/etc. in the
output comes from exactly here.
Press ▶ to walk the whole cycle from the first SYN to the final TIME_WAIT.
The server opened a listening socket on port 443 (see
port). The OS stack knows: "if a packet arrives here, hand it to this
application". The server state is LISTEN.
The client is still in CLOSED, with no sockets open. When the
application calls connect(), the stack starts the active handshake.
recap
What to remember:
seq and wants to be sure the peer received it.
Without this an attacker could inject packets into other people's
connectionsSYN_SENT stuck forever = the server is not answering (a firewall
drops it, or the port is closed). SYN_RECEIVED stuck forever on
the server = the incoming connection queue is fullss is the trace of heavy downloading, not a leakIf you want to watch the states by hand, there is [[tcp-states|an article about all the states]] and a lab on tcp-handshake with tcpdump.