TCP Handshake Sequence Diagram

Before a browser can send an HTTPS request, TCP must open a connection. This page draws one complete TCP conversation as a UML sequence diagram: the 3-way handshake, a request and response with real sequence and acknowledgement numbers, and the 4-way close. A second diagram shows the two most common failures.


1. The Diagram

TCP Connection: Handshake, Data, Close 3-way handshake data transfer 4-way close Client10.0.0.5:51514 Server203.0.113.10:443 SYN seq=1000 SYN-ACK seq=5000 ack=1001 ACK seq=1001 ack=5001 PSH,ACK seq=1001 len=100 (request) ACK ack=1101 PSH,ACK seq=5001 len=1400 (response) ACK ack=6401 FIN,ACK seq=1101 ack=6401 ACK ack=1102 FIN,ACK seq=6401 ack=1102 ACK ack=6402 connection ESTABLISHED client closes first SYN_SENT ESTABLISHED FIN_WAIT_1 FIN_WAIT_2 TIME_WAIT (2 x MSL) LISTEN SYN_RCVD ESTABLISHED CLOSE_WAIT LAST_ACK CLOSED

Time flows downward. Each arrow is one TCP segment with its flags and sequence numbers; the labels at the sides are the TCP states (RFC 9293) each host is in after that segment.

  1. SYN. The client picks a random initial sequence number (here 1000) and moves to SYN_SENT.
  2. SYN-ACK. The server, in LISTEN, replies with its own sequence number (5000) and acknowledges 1001, meaning "I expect your byte 1001 next."
  3. ACK. The client acknowledges 5001. Both sides are now ESTABLISHED. That is one round trip before any data moves.
  4. Data. The client sends a 100-byte request; the server acknowledges 1101 and sends a 1400-byte response, which the client acknowledges with 6401.
  5. FIN from the client. The client is done sending; it enters FIN_WAIT_1, then FIN_WAIT_2 once the ACK arrives. The server enters CLOSE_WAIT and may still send.
  6. FIN from the server. The server closes its half (LAST_ACK). The client's final ACK puts it into TIME_WAIT for twice the maximum segment lifetime so stray segments die out.

2. How to Read It

How to Read a UML Sequence Diagram Participant host or role lifeline (time flows down) message / packet reply (dashed) key step error / failure loop / alt / opt combined fragment repeats or branches

Hosts are participants with a dashed lifeline. Solid arrows carry new data or control flags; dashed arrows are pure acknowledgements. Dashed boxes are combined fragments that group the three phases.

3. Variant: Failure Paths

Failure Paths: Closed Port and Lost SYN alt: port closed alt: packet loss Client 10.0.0.5 Server 203.0.113.10 SYN dport=8443 RST,ACK (port closed) connect() fails: ECONNREFUSED SYN dport=443 (dropped) wait 1 s (RTO) SYN retransmit #1 (dropped) wait 2 s, then 4 s, 8 s ... SYN retransmit #2 SYN-ACK ACK connection refused ESTABLISHED (late) no listener

Two alt fragments show what goes wrong. If nothing listens on the port, the server answers with RST and the client gets "connection refused" at once. If a firewall silently drops the SYN, the client retransmits with a doubling timeout (1 s, 2 s, 4 s ...), which is why a blocked port looks like a hang rather than an error.

4. Pros, Cons, and When to Use

Pros

Cons

When to use

When not to use

5. As Diagrams-as-Code

Mermaid sequenceDiagram (renders in GitHub, GitLab and most docs tools):

sequenceDiagram
    participant C as Client 10.0.0.5:51514
    participant S as Server 203.0.113.10:443
    rect rgb(40, 40, 10)
    C->>S: SYN seq=1000
    S->>C: SYN-ACK seq=5000 ack=1001
    C->>S: ACK seq=1001 ack=5001
    end
    C->>S: PSH,ACK seq=1001 len=100
    S-->>C: ACK ack=1101
    S->>C: PSH,ACK seq=5001 len=1400
    C-->>S: ACK ack=6401
    C->>S: FIN,ACK seq=1101
    S-->>C: ACK ack=1102
    S->>C: FIN,ACK seq=6401
    C-->>S: ACK ack=6402
    Note over C: TIME_WAIT (2 x MSL)