CloudMyLab Blog

Understanding the TCP/IP 3-Way Handshake

Written by Tanishka Mogha | Oct 14, 2025 8:44:28 PM

Few things are as fundamental to reliable network communication as the TCP 3-Way Handshake. This process is how your devices build trust and sync up before they start sending data. It’s not just about getting basic connections working; it’s the essential groundwork for countless tools and protocols you use daily, like Nmap, HTTP, and DNS, all of which rely on TCP's reliability.

This guide breaks down how the TCP handshake works, why it's so vital for network reliability, how it's different from UDP, and how it powers real-world network protocols and even security scanning techniques.

Table of contents

 

What Is the TCP/IP 3-Way Handshake?

Transmission Control Protocol (TCP) is designed to give you reliable, ordered, and error-checked communication between two devices. Unlike connectionless protocols like UDP, which just fire off data without any agreement, TCP requires a structured process to formally set up a session before any application data is sent. That's where the 3-Way Handshake comes in.

It's a simple, three-step negotiation:

  • The client proposes a connection and sends its Initial Sequence Number (ISN) using a SYN packet.
  • The server acknowledges the client's ISN and sends its own ISN back with a SYN-ACK packet.
  • The client sends a final ACK packet to acknowledge the server's ISN.

Once this final ACK is sent, both the client and server move to the ESTABLISHED state, and reliable application data can start flowing.

How does the SYN, SYN-ACK, ACK sequence work?

This quick exchange is the core of the TCP handshake. It aligns the state on both the client and server, making sure subsequent data arrives in the correct order without issues.

Step

Direction

Flags

Purpose

1

Client → Server

SYN

Propose connection, send client's Initial Sequence Number (ISN), suggest options (like MSS).

2

Server → Client

SYN, ACK

Acknowledge client's ISN, send server's ISN, negotiate options.

3

Client → Server

ACK

Acknowledge server's ISN. Both sides are now ESTABLISHED.

The final ACK usually doesn't carry any application data. The Initial Sequence Numbers (ISNs) are randomized for security, and each side always acknowledges the other's ISN plus one. This simple math keeps the byte streams synchronized, ensuring your data arrives in order.

Visualizing the Handshake

This simple exchange ensures both ends are synchronized — like a firm handshake before beginning a conversation.

SYN (Synchronize) - Initiation

The client starts the connection by sending a TCP packet with the SYN flag set. This packet also includes a proposed Initial Sequence Number (ISN). It's basically saying: "I’d like to start a conversation."

This first step confirms the client is ready and requests synchronization.

SYN-ACK (Synchronize–Acknowledge) - Response

The server gets the SYN, then replies with a packet that has both the SYN and ACK flags set.

  • The ACK acknowledges the client’s initial request.
  • The SYN signals that the server is ready, providing its own ISN.

At this point, the server has set aside resources for the connection and is waiting for the client's final confirmation.

ACK (Acknowledge) - Confirmation

Finally, the client sends one last ACK packet back to the server. This confirms it received the server’s SYN-ACK. Once this happens, the connection is officially open, and both sides can start sending data reliably.

How do connection states change during the TCP 3-way handshake?

The handshake isn't just about packet exchanges; it's about moving both the client and server through defined connection states:

Phase

Client State

Server State

Before connection

CLOSED

LISTEN

Step 1: SYN sent

SYN-SENT

LISTEN

Step 2: SYN-ACK received

ESTABLISHED (after sending its ACK)

SYN-RECEIVED

Step 3: ACK sent

ESTABLISHED

ESTABLISHED

This synchronized sequence ensures both ends agree on sequence numbers and are ready to communicate, setting the stage for ordered, reliable data transfer.

What is the purpose of the TCP 3-way handshake?

The handshake is your connection's essential setup and early-warning system. It confirms both ends can send and receive data, syncs their sequence numbers for ordering, and negotiates important performance options. If handshake timing is off or failures spike, users will feel the pain (slow websites, failed connections) even before any actual application data is sent. Monitoring TCP handshake health is fundamental to ensuring your applications are available.

Key Handshake Benefits

  • Reliability: Confirms both client and server are reachable, listening, and ready.
  • Synchronization: Aligns Initial Sequence Numbers (ISNs) for ordered data and handling missing/out-of-order segments.
  • Error Control & Performance: Negotiates parameters like MSS (Maximum Segment Size) and Window Scale to optimize network performance and prevent issues like IP fragmentation.
  • State Management: Clearly establishes the connection in the ESTABLISHED state, enabling proper connection tracking and a graceful shutdown later (using a separate FIN exchange).

Without this handshake, TCP couldn't guarantee data integrity—a trade-off protocols like UDP deliberately make for speed and lower overhead.

Why It Matters for Reliability

The handshake confirms both endpoints can send and receive, aligns sequence numbers, and negotiates options that influence performance. If handshake timing is erratic or failures spike, users experience slow page loads or failed connections before any application data appears. Watching handshake health is an early-warning signal for network, security, or server readiness issues.

TCP vs. UDP: How does the TCP 3-way handshake differ from a UDP connection?

Both TCP and UDP operate at the transport layer, but they make different promises. TCP pays an upfront cost with its 3-way handshake to guarantee order, reliability, and recovery from packet loss. UDP skips this ceremony for speed and minimal overhead.

Feature

TCP

UDP

Connection Type

Connection-oriented

Connectionless

Handshake

Yes (3-Way)

No

Reliability

GuaranGuaranteed delivery (retransmits)

Best-effort (no guarantees)

Ordering

Maintains sequence

No order guarantee

Overhead

Higher (larger headers)

Lower (smaller headers)

Common Uses

HTTP, HTTPS, SMTP, SSH

DNS, VoIP, Gaming, Streaming

What’s a practical example of the TCP 3-way handshake in a client–server exchange?

Every time you browse a website, your browser initiates a TCP handshake to confirm the server’s readiness before sending HTTP requests.
Tools like Nmap, on the other hand, use synthetic handshake signals to probe servers for open ports or vulnerabilities, often without completing the connection at all.

Example 1: HTTP (Over TCP)

  1. Browser (client) sends SYN to the web server (port 80/443).
  2. Server responds with SYN-ACK.
  3. Browser sends ACK, and the connection is established.
  4. The HTTP GET request follows.

This ensures that the connection is stable and ready for data transfer.

Example 2: DNS (Over UDP)

  1. DNS queries are typically sent via UDP (port 53) for faster resolution.
  2. There’s no handshake, the client sends a single query and the server replies.
  3. However, for larger responses or critical operations (like zone transfers), DNS switches to TCP, performing a full 3-way handshake.

How Nmap Leverages TCP for Network Scanning

Nmap (Network Mapper) is a powerful tool for network discovery and security auditing. It uses TCP handshake behavior to detect open ports and map network states efficiently.

TCP Connect Scan (-sT)

Performs the complete 3-way handshake with a target port.

  • If the handshake succeeds, the port is marked as open.
  • It’s reliable but easily logged since it establishes a full connection.

SYN Scan (Half-Open Scan, -sS)

Sends only a SYN packet and waits for a response.

  • SYN-ACK → Port open
  • RST → Port closed

Because the handshake is never completed, SYN scans are faster and stealthier — ideal for reconnaissance.

By analyzing TCP flag responses (SYN, ACK, RST), Nmap identifies whether ports are open, closed, or filtered, helping network engineers pinpoint security gaps and optimize configurations.

Real-World Perspective for Network Engineers

Understanding the handshake is vital for:

  • Troubleshooting: Diagnosing packet loss, high latency, or TCP retransmissions.
  • Security: Designing effective firewall rules and ACLs. Spotting suspicious connection attempts.
  • Performance Tuning: Understanding MSS and window scaling helps optimize throughput.
  • Forensics: Recognizing handshake packets in captures helps trace connection issues.

The handshake is the start of every TCP session. Seeing these packets in Wireshark or firewall logs gives you a direct look into your network's behavior.

Which metrics help troubleshoot TCP 3-way handshake issues?

Begin with the handshake RTT (SYN→SYN‑ACK) and how quickly the final ACK follows; both are your readiness pulse. Keep an eye on SYN retransmit rate and overall handshake failure rate (connections that never reach ESTABLISHED). If RSTs spike, you’re being refused rather than delayed. Compare current time‑to‑ESTABLISHED against a known baseline to separate path noise from real regressions. Finally, confirm negotiated options (MSS, Window Scale, SACK), because they materially affect throughput and loss recovery once the session starts moving data.

How do you troubleshoot TCP 3-way handshake failures?

  • No SYN-ACK from the server? Client keeps retrying SYN? Usually a policy or reachability problem. Check firewalls/security groups blocking the SYN, closed ports, asymmetric routing, or PMTU issues.
  • Immediate RST from the server after client SYN? The service probably isn't listening, the connection was refused, or a host policy blocked it.
  • Connection takes too long to ESTABLISHED? Look for packet loss, high latency, or an overloaded server. Repeated SYN or SYN-ACK retransmissions are a bad sign.
  • Data transfer stalls after handshake? Check MTU/MSS mismatches (causing fragmentation), disabled SACK (hurting loss recovery), or overzealous middleboxes (proxies/firewalls) messing with packets.

Quick Checks: Make sure the service is listening on the server (ss -ltnp or netstat -plnt on Linux). Check your firewall rules. Test PMTU discovery. If all else fails, capture packets on both ends to see what's happening on the path.

Conclusion

The TCP/IP 3-Way Handshake is the foundation for reliable, ordered communication. By understanding how SYN, SYN-ACK, and ACK packets work together, you as a network engineer can get much better at diagnosing connectivity problems, securing your network, and tuning application performance. Tools like Nmap rely on these principles for smart network scanning, proving that knowing this handshake is key to managing and optimizing modern networks.

 

FAQs

What is the purpose of the TCP 3-way handshake?

Its primary purpose is to establish a reliable, synchronized connection between two devices. It confirms both are reachable and ready for data exchange and synchronizes their sequence numbers for ordered data delivery.

What are the 3 steps of the TCP handshake?

The three steps are SYN, SYN-ACK, and ACK. The client sends a SYN to propose a connection. The server acknowledges with a SYN-ACK. The client confirms with a final ACK, moving both sides to the ESTABLISHED state for reliable data flow.

What happens after the handshake?

The TCP connection is in the ESTABLISHED state. Congestion control mechanisms begin to govern data flow, and application data begins to flow in an ordered, reliable stream. Later, the connection is terminated with a separate four-packet FIN/ACK exchange.

What happens if the handshake fails?

If the 3-way handshake fails, a TCP connection will not be established, and no application data can be transferred.

How does Nmap use the handshake to detect open ports?

Nmap sends SYN packets to target ports and watches for SYN-ACK replies to identify open ports. It may complete the full handshake (Connect scan) or stop halfway (stealthy SYN scan) to gather this information.

How does window scaling affect performance?

Window Scaling is a TCP option extending the receive window size beyond 65,535 bytes, crucial for high throughput on high-bandwidth, high-latency paths. Larger windows allow more data "in flight," fully utilizing available bandwidth. Window Scaling is negotiated during the 3-way handshake.

How does understanding the handshake aid network security?

Understanding the handshake is critical for configuring firewalls and intrusion detection systems (IDS). It allows you to recognize and block abnormal connection attempts (like SYN floods) and conduct more effective network forensics.

Does IPv6 change the handshake?

No. TCP’s connection setup is identical over IPv4 or IPv6. The IP layer changes addressing and header format, but the TCP state machine and the three-packet SYN, SYN-ACK, ACK ceremony remain the same.

Does TLS replace the TCP handshake?

No. TLS/SSL runs on top of TCP. First, a reliable TCP connection is established using the standard 3-way handshake. Then, the TLS handshake begins over that established TCP connection to negotiate ciphers and exchange keys before any encrypted application data flows.

What is a SYN flood attack?

A SYN flood is a Denial of Service (DoS) attack where an attacker sends a large number of SYN packets but never completes the handshake with the final ACK. This forces the server to keep many half-open connections in its memory, eventually exhausting resources and preventing new, legitimate connections.

How can SYN flood attacks be mitigated?

SYN floods can be mitigated by enabling features like SYN cookies on the server, allowing it to handle SYN requests without immediately allocating memory. Other methods include right-sizing and monitoring the SYN backlog, applying per-IP or per-prefix rate limits on firewalls/load balancers, and using upstream DDoS protection to filter out abusive traffic.

How is the initial sequence number (ISN) generated?

Stacks use a randomized, time‑varying function (per RFC guidance, e.g., RFC 6528) that mixes a secret with the 4‑tuple (src/dst IP and port) so ISNs are unpredictable and resistant to spoofing and replay, while still monotonically advancing over time.