The TCP three-way handshake is the three-packet exchange that opens a TCP connection: the client sends SYN with its initial sequence number, the server replies SYN-ACK with its own initial sequence number and an acknowledgement, and the client sends ACK to confirm. It synchronizes sequence numbers in both directions, agrees on options such as maximum segment size, and proves both sides can send and receive before any application data flows.
Key takeaways
- The handshake is SYN, SYN-ACK, ACK: three packets that synchronize sequence numbers in both directions.
- Each side picks a random initial sequence number, and a SYN consumes one sequence number, so acknowledgements are always one higher.
- The handshake is also a security target: SYN floods exhaust a server's half-open connection queue, which is why SYN cookies exist.
Every time your browser loads a web page, your email client fetches mail, or an SSH session opens, a quiet three-packet conversation happens first. It takes a fraction of a second, and without it no reliable data can flow at all.
What is the TCP three-way handshake?
The TCP three-way handshake is the three-packet exchange that opens a TCP connection. The client sends SYN, the server replies SYN-ACK, and the client sends ACK — and only then is the connection established.
TCP, defined in RFC 9293, is a connection-oriented, reliable, ordered, full-duplex byte-stream protocol. It runs at the transport layer, layer 4, and it must establish shared state before data can flow: sequence numbers, window sizes and negotiated options on both sides.
The handshake is how two hosts create that shared state. Each side must announce its own initial sequence number and have the other side acknowledge it. Because each direction has its own sequence space, that takes a SYN and an acknowledgement in each direction — and the middle two steps are combined into one packet, which is why it takes three packets instead of four.
What are the three steps of the handshake?
The three steps are SYN, SYN-ACK and ACK. Each one moves the connection through a state in TCP's finite state machine, from CLOSED and LISTEN to ESTABLISHED on both sides.
Step 1: SYN
The client sends a segment with the SYN flag set and no payload. It chooses an initial sequence number, call it x, and moves from CLOSED to SYN-SENT.
This segment also carries the options the client supports, such as maximum segment size, window scale, SACK-permitted and timestamps. The server must be in LISTEN, waiting for connections on a port such as 443 for HTTPS.
Step 2: SYN-ACK
The server replies with both SYN and ACK set. It picks its own initial sequence number y, acknowledges x + 1, and moves to SYN-RECEIVED. It also includes its own options in the reply.
The server allocates a Transmission Control Block entry in its half-open SYN backlog queue while it waits for the final ACK. That queue is exactly what SYN flood attacks target, as explained below.
Step 3: ACK
The client sends a plain ACK with sequence number x + 1 and acknowledgement number y + 1. The client is now ESTABLISHED, and when the server receives this ACK, it moves to ESTABLISHED too.
The client can start sending data after it has processed the SYN-ACK, and it may piggyback data on this final ACK. The server cannot treat the handshake as complete until it receives the final ACK.
Why do sequence numbers matter?
Sequence numbers count bytes, not packets. Each side picks a random 32-bit initial sequence number, and every byte sent afterwards has a number. An ACK carries the next byte the receiver expects, which is how TCP knows what arrived and what needs retransmission.
A SYN consumes one sequence number, which is why the acknowledgement is one higher than the ISN. In the RFC 9293 example, the client picks 100, the server acknowledges 101, and the client's first data byte still starts at 101. A standalone ACK consumes none, so the numbering stays consistent.
Randomized ISNs matter for security. If an attacker could guess the sequence numbers, they could inject forged segments into a connection. RFC 9293 therefore requires a clock-driven ISN, and modern stacks add randomness so attackers cannot easily predict it.
Sequence numbers also protect against old duplicate segments. A delayed SYN from a long-finished connection should not be mistaken for a fresh one, and the handshake's acknowledgement exchange is what prevents that confusion.
Why not two packets? Why does TCP need three?
Two packets would leave one side unconfirmed. The final ACK tells the server that the client received the server's chosen sequence number. Without that confirmation, an old delayed SYN could be mistaken for a fresh connection.
RFC 9293 describes avoiding confusion from old duplicate connection requests as the main reason for the three-way handshake. Packet loss does not change the protocol into a two-message exchange: if the last ACK is lost, the server retransmits its SYN-ACK while it awaits confirmation.
Think of it as two people agreeing on a meeting time. One proposes a time, the other confirms it and proposes a return time, and the first confirms that too. Until the last confirmation arrives, one side does not know the plan is agreed.
What gets negotiated during the handshake?
The handshake is the only place where several TCP options can be agreed. They appear in the TCP header options field of the SYN and SYN-ACK, and they shape the whole connection that follows.
| Option | What it does | Why it matters |
|---|---|---|
| MSS | Maximum segment size each side will receive | Avoids fragmentation and wasted packets |
| Window scale | Lets the 16-bit window describe windows over 64 KB | Essential for fast, long-distance links |
| SACK-permitted | Allows selective acknowledgement of received blocks | Faster recovery when some packets are lost |
| Timestamps | Adds a timestamp to each segment | Better round-trip measurement and protection against wrapped sequence numbers |
| TCP Fast Open | Allows data in the SYN on repeat connections | Saves a round trip, with strict security conditions |
If an option is not offered in the SYN, it cannot be used later in that connection. That is why the handshake matters for performance as well as for correctness: the choices made in those three packets set the speed of everything that follows.
How does TCP differ from UDP?
TCP is connection-oriented, reliable and ordered. UDP is connectionless: it sends datagrams without a handshake, without ordering and without retransmission. That makes UDP faster, but the application must handle loss itself.
Use TCP when every byte must arrive in order — web pages, email, file transfers and SSH. Use UDP when speed matters more than completeness, such as live video, voice calls and DNS lookups, where a lost packet is better than a delayed one.
QUIC, the protocol behind HTTP/3, is built on UDP but adds its own handshake and reliability on top. It still needs to agree on keys and parameters before data flows, just in fewer round trips than TCP plus TLS.
What does the handshake look like in Wireshark?
In Wireshark, the handshake is three packets with distinctive flag values. A SYN shows as 0x02, a SYN-ACK as 0x12, and a bare ACK as 0x10. Those are not arbitrary codes: the TCP control flags live in a single byte, and each bit position is one flag.
A SYN-ACK is simply a packet with two bits set at once: SYN is 0x02 and ACK is 0x10, so 0x12 is their sum. A reset that acknowledges the aborted sequence is RST plus ACK, or 0x14. Learning to read those hex values makes packet captures much easier to follow.
Filter with tcp.flags.syn == 1 to find handshakes, or tcp.stream eq 0 to follow the first connection in a capture. If you see many SYNs with no matching SYN-ACK, the server may be down, firewalled, or under a SYN flood.
To check the public side of a site you manage, run our security headers checker — it shows whether the TLS layer that sits on top of TCP is configured correctly.
For a packet-by-packet walkthrough with capture examples, see OpenPCAP's TCP handshake guide.
Another clear explanation of why three packets are needed is in The Packet to Prompt's handshake explainer.
How do attackers abuse the handshake?
The handshake's weak point is the server's SYN backlog. In a SYN flood, an attacker sends many SYN packets and never completes the handshake, so the server keeps each half-open connection in its queue until it times out.
When the backlog fills, legitimate SYNs are dropped and real users cannot connect. That is a denial of service, even though the server itself is still running.
The standard defense is SYN cookies: instead of storing state for each half-open connection, the server encodes the connection details into the sequence number of its SYN-ACK, and only creates state when the final ACK arrives with the right number. Larger backlogs, SYN rate limiting and firewall rules help too.
Attackers can also abuse the handshake for port scanning. A SYN scan sends a SYN to many ports and watches for SYN-ACK (open) versus RST (closed), without ever completing the handshake. That is why unexpected SYN traffic in your logs deserves a look.
How does a TCP connection close?
Closing is a four-way exchange, because each direction closes independently. One side sends FIN, the other ACKs it, then the other side sends its own FIN, and the first side ACKs that.
Each FIN consumes one sequence number, just like a SYN. After the final ACK, the side that closed first waits for twice the maximum segment lifetime, or 2 × MSL, in TIME_WAIT state. That wait ensures delayed packets from the old connection do not confuse a new connection using the same ports.
You can see the full lifecycle in a capture: SYN, SYN-ACK, ACK, data, then FIN, ACK, FIN, ACK. If you see a RST instead, the connection was aborted abruptly — for example, because the application crashed or a firewall rejected it.
How do you troubleshoot a failed handshake?
Start with the simplest test: can you reach the port at all? A connection that hangs after SYN usually means a firewall is silently dropping packets, while an immediate RST usually means nothing is listening on that port.
- No SYN-ACK at all: check the server is running, the port is open, and no firewall is dropping SYNs.
- RST straight away: the port is closed, or a firewall is actively rejecting the connection.
- SYN-ACK but no ACK: the client's ACK may be blocked, or the client may have moved on. Check both directions, not just one.
- Slow handshake: look at round-trip time and packet loss. The handshake costs one round trip before any data, so on a 100 ms link it is visible to users.
- Many half-open connections: check for a SYN flood or a misconfigured client that opens connections and abandons them.
On Linux, ss -t shows connection states including SYN-SENT and SYN-RECV, and netstat -s shows TCP statistics. In Wireshark, the expert information flags retransmissions and out-of-order packets that point to the cause.
If the problem is on your home network rather than the server, our guide to how to secure your Wi-Fi covers router and network checks that help rule out local causes.
The bottom line
The TCP three-way handshake is SYN, SYN-ACK, ACK: three packets that synchronize sequence numbers in both directions, agree on options, and prove both sides can send and receive. Two packets would leave one side unconfirmed, and old delayed requests could be mistaken for new connections.
Understanding it helps you read packet captures, troubleshoot slow or failed connections, and spot SYN floods before they take a server down. For the next layer up, check how TLS builds on this handshake — and verify your own site's TLS setup with our free SSL checker.
Frequently asked questions
What is the TCP three-way handshake in simple terms?
It is the three-message introduction two computers make before exchanging data over TCP. The client says hello and proposes a starting number, the server replies with its own number and confirms the first, and the client confirms the second. Only then can data flow.
Why does TCP need three packets instead of two?
Because each direction needs its own synchronization. The server must confirm it received the client's initial sequence number, and the client must confirm it received the server's. A two-packet exchange would leave one side unconfirmed, and old delayed connection requests could be mistaken for new ones.
What is SYN, SYN-ACK and ACK?
SYN is the synchronize flag that starts a connection and proposes an initial sequence number. SYN-ACK is a packet with both SYN and ACK set, used by the server to reply. ACK is the acknowledgement flag that confirms receipt of the other side's sequence number.
What is an initial sequence number?
An initial sequence number, or ISN, is the random 32-bit starting number each side picks for its byte stream. Sequence numbers count bytes, so the first data byte is ISN plus one, because the SYN itself consumes one sequence number.
How long does the three-way handshake take?
About one round trip between client and server. On a fast local network that is under a millisecond, and on a long-distance internet path it can be tens or hundreds of milliseconds. That is why reusing connections and using TLS session resumption matter for web performance.
What is a SYN flood attack?
A SYN flood is a denial-of-service attack that sends many SYN packets and never completes the handshake. The server keeps each half-open connection in its SYN backlog, which fills up and stops legitimate connections. SYN cookies are a common defense.
Does UDP have a handshake?
No. UDP is connectionless and sends datagrams without establishing a connection first. That makes it faster, but there is no reliability, ordering or handshake, so applications must handle loss themselves.
Can data be sent during the handshake?
Yes, in limited cases. The client can piggyback data on the final ACK, and TCP Fast Open allows data in the SYN itself under strict conditions. In the ordinary handshake, though, application data starts after the third packet.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against RFC 9293, the current TCP specification, and packet-level explanations of the handshake and its state machine.
How we write and fact-check →