Internet Basics & History

The TCP Three-Way Handshake, Step by Step

Walk through SYN, SYN-ACK and ACK, see what each carries and learn to diagnose connections that never finish opening.

Key takeaways

  • The handshake has three messages: SYN, SYN-ACK and ACK.
  • Each side picks an initial sequence number and the other side acknowledges it, which is why two messages wouldn't be enough.
  • Setting up a connection costs about one network round trip before application data can flow.
  • A reset usually means nothing is accepting the connection, while silence usually means packets are being lost or filtered.

The TCP three-way handshake is the short exchange that opens a connection: the client sends a SYN, the server answers with a SYN-ACK and the client replies with an ACK. In those three messages, both sides confirm they can send and receive, agree on starting sequence numbers and announce the options they support.

Only after that does your request for a page, a file or a mailbox travel. When a connection "hangs" or is "refused," the handshake is usually where the story ends, so it pays to know what each step should look like.

What TCP needs to agree on

TCP promises that bytes arrive complete and in order, as compared with the alternative in TCP vs. UDP. To keep that promise, each end numbers the bytes it sends. Before any data moves, each side must learn where the other's numbering starts.

A connection is identified by four values: source IP address, source port, destination IP address and destination port. The server listens on a well-known port, such as 443 for HTTPS, and the client picks a temporary port. That combination lets one server hold many connections on the same port at once, an idea covered in ports explained.

Control information travels in the TCP header as single-bit flags. Three matter here: SYN (synchronize sequence numbers), ACK (the acknowledgment field is valid) and RST (reset, abort this connection).

The three messages

Say the client chooses the starting number 1000 and the server chooses 5000. Real values are chosen to be hard to predict.

Opening a TCP connection (example numbers)
Client Server SYN, seq=1000 1 I want to connect; my numbering starts here SYN-ACK, seq=5000, ack=1001 2 Agreed; here is my start, and I got yours ACK, seq=1001, ack=5001 3 I got yours too Application data 4 For HTTPS, the TLS handshake begins Client Server SYN, seq=1000 1 I want to connect; my numbering starts here SYN-ACK, seq=5000, ack=1001 2 Agreed; here is my start, and I got yours ACK, seq=1001, ack=5001 3 I got yours too Application data 4 For HTTPS, the TLS handshake begins

Step 1: SYN

The client sends a segment with the SYN flag set, carrying its initial sequence number and a list of options. It contains no web request yet. The client now waits in a state called SYN-SENT.

Step 2: SYN-ACK

The server, which has been waiting in the LISTEN state, replies with both SYN and ACK set. Its acknowledgment number is the client's number plus one, because the SYN itself counts as one position in the sequence. It also supplies its own starting number and options. The server's side of the connection is now SYN-RECEIVED.

Step 3: ACK

The client acknowledges the server's number plus one. When this arrives, both sides are ESTABLISHED. The client can attach data to this third segment or send it immediately afterward.

Why three and not two

With only a SYN and a reply, the client would know the server heard it, but the server wouldn't know whether its own starting number arrived. The third message confirms the second direction.

It also protects against stale packets. A delayed SYN from an old attempt could reach a server long after the client has given up. The server would answer, and the client, not expecting that reply, would reject it with a reset. No phantom connection is established.

In principle there are four pieces of information to exchange: each side's number and each side's confirmation. The server's confirmation and its own number fit in one segment, so four steps fold into three.

Options negotiated along the way

The SYN and SYN-ACK are the moment to declare capabilities. Common options include:

Option What it does
Maximum segment size (MSS) States the largest chunk of data each side wants in one segment
Window scale Allows a much larger receive window for fast or long-distance links
Selective acknowledgment permitted Lets the receiver report exactly which blocks arrived
Timestamps Helps measure round-trip time and reject old segments

An option generally takes effect only if both sides indicate support. The segment size is tied to the largest packet the path can carry, and mismatches there cause odd failures where connections open but large transfers stall. That is the subject of MTU, MSS and fragmentation.

What it costs in time

The client can't send its request until the SYN-ACK returns, so every new connection spends one round trip on setup. If the round trip to a server is 20 milliseconds, that's barely noticeable. If it's 200 milliseconds across an ocean or a slow link, it adds up, especially when TLS needs its own round trip on top.

Software hides much of this by reusing connections for many requests, opening them in advance and placing servers close to users. TCP Fast Open (RFC 7413) allows some data in the SYN for repeat visitors, though support through real networks has been uneven. Newer designs combine transport and encryption setup, as QUIC does.

Sequence numbers after the handshake

The numbers agreed in the handshake set up the bookkeeping for everything that follows. A sequence number refers to a position in the byte stream, and an acknowledgment number says "I have everything before this; send me this position next."

Continue the example. The client's first data segment starts at 1001. If it carries a request of 300 bytes, it covers positions 1001 through 1300, and the server acknowledges 1301. If the server then sends 1,000 bytes starting at 5001, the client answers with 6001. Each direction counts independently.

Packet analyzers often display relative sequence numbers, starting each side at zero, to make traces easier to read. If your capture shows seq 0 on a SYN, that is the tool's presentation, not the value on the wire.

Starting numbers are unpredictable for a security reason. An outsider who can't see the traffic would need to guess them to inject convincing segments into a connection or to fake a handshake from an address they don't control. RFC 9293 expects implementations to generate them in a way that resists guessing. This is a limited protection: someone who can observe the path sees the numbers, which is why applications still need TLS to protect content and verify identity.

Middle devices and the handshake

Many connections don't run straight from client to server. A load balancer, proxy or content delivery network may complete the handshake with you and open a second connection to the real server behind it. A home router that performs address translation watches the SYN to create a mapping, then uses it for the replies.

This explains some confusing symptoms. The handshake can succeed instantly while the page fails, because the machine that answered is only the front door. Firewalls that track connections may also discard an idle one after a timeout, so the next packet meets a reset or silence even though neither end closed it. Applications guard against this with keepalive messages.

When the handshake fails

Three outcomes tell you different things.

Reading a failed connection attempt
Did a reset (RST) come back right away? YES You reached something, but the port has no listener or a rule rejects it. 1 NO Did SYNs repeat with no answer at all? YES Check the address, routing, a dropping firewall or a host that is down. 2 NO Did SYN-ACKs repeat from the server? YES The reply or the final ACK is being lost. Look at the return path. 3 NO Handshake completed TCP is fine. Look at TLS, the application or the server's response. Did a reset (RST) come back right away? 1 IF YES You reached something, but the port has no listener or a rule rejects it. NO Did SYNs repeat with no answer at all? 2 IF YES Check the address, routing, a dropping firewall or a host that is down. NO Did SYN-ACKs repeat from the server? 3 IF YES The reply or the final ACK is being lost. Look at the return path. NO Handshake completed TCP is fine. Look at TLS, the application or the server's response.

Browsers translate these into messages such as "connection refused" or "connection timed out," compared in what each connection error means.

When nothing answers, the client retransmits the SYN several times, waiting longer after each attempt. That's why a timeout can take many seconds while a refusal appears instantly.

Watching it yourself

You can test whether a handshake succeeds. In Windows PowerShell:

Test-NetConnection example.com -Port 443

On macOS or Linux, where netcat is available:

nc -vz example.com 443

To see the individual segments, capture your own traffic. On Linux, with administrator rights:

sudo tcpdump -n -i any 'tcp port 443 and (tcp[tcpflags] & (tcp-syn|tcp-ack) != 0)'

That filter also matches later segments carrying ACK, so look for the first lines of each new connection, where the flags show as [S], [S.] and [.]. Wireshark presents the same thing graphically. Capture only on systems and networks you're authorized to inspect.

You can also list connection states. ss -tan on Linux, or netstat -an on Windows and macOS, shows entries such as LISTEN and ESTABLISHED.

Half-open connections and SYN floods

After sending a SYN-ACK, a server has to remember something about the pending connection. A SYN flood is a denial-of-service attack that sends large numbers of SYNs and never completes them, filling that waiting list so legitimate clients can't get in.

Defenses are well established and described in RFC 4987. With SYN cookies, the server encodes the necessary information into its initial sequence number and stores nothing until a valid ACK comes back. Operating systems offer this along with backlog and timeout settings, and hosting providers, firewalls and content delivery networks can absorb or filter floods upstream. If you run a public server, confirm these protections are enabled and monitor for a sudden pile of half-open connections.

Closing the connection

Opening takes three messages. A graceful close usually takes four: one side sends FIN, the other acknowledges it, then sends its own FIN, which is acknowledged in turn. Each direction closes independently, and the middle two messages are sometimes combined.

The side that closes first then waits in TIME-WAIT for a while. This ensures the final acknowledgment gets through and that late packets from the old connection can't be mistaken for part of a new one. Seeing many TIME-WAIT entries on a busy machine is normal. A reset, by contrast, ends things immediately and discards unsent data.

Common questions

Does UDP have a handshake?

No. UDP sends data without setup. Protocols built on it may add their own handshake when they need one.

Is the handshake encrypted?

No. The TCP header, including the flags and sequence numbers, is visible to networks along the path. Encryption begins afterward with TLS, which protects the data but not the TCP header.

Does a completed handshake mean the website works?

It means a program accepted the connection on that port. The certificate could still be invalid, the application could return an error or a proxy could be answering in front of a broken server.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Internet Basics & History →

Latest news on this topic