Routelearn.net
Course menu

Unit 7: TCP, UDP and PortsLesson 7.1.3 (3 of 20 in this unit)36 of 84 in the Network Fundamentals course

The three-way handshake

SYN, SYN-ACK, ACK: how two hosts agree to talk before sending any data.

Beginner · 6 min read

TCP three-way handshake is the exchange of three segments, SYN, SYN-ACK and ACK, that opens a TCP connection. It confirms that both sides are ready to communicate and lets each learn the other's initial sequence number before any data is sent.

In simple terms: Before talking, the two sides check in: “Can I talk to you?”, “Yes, and can I talk to you too?”, “Yes.” After those three messages, data can flow both ways.

A situation

Before you tell a friend something important on the phone, you check that they are there: “Hello, can you hear me?” “Yes, can you hear me?” “Yes.” Only then do you start talking. TCP does the same thing before it sends any data. This short exchange is the three-way handshake.

What it is

The handshake is made of three segments, each named after the flags it carries:

  1. SYN: the client asks to open a connection and sends its initial sequence number (ISN), the starting point for numbering its bytes.
  2. SYN-ACK: the server agrees. It sends its own ISN (SYN) and acknowledges the client's SYN (ACK).
  3. ACK: the client acknowledges the server's SYN. The connection is now open in both directions.

Press play to watch the handshake.

CLIENT
SYN
SYN-ACK
ACK
SERVER

The numbers in each message

Real ISNs are large, hard-to-guess numbers. To keep this example readable, the client starts at 1000 and the server at 5000. Tap a message to see its fields.

Step 1 of 3 · SYN
Client
192.168.1.20:52000
Internet
Web server
203.0.113.10:443
Connection ESTABLISHED
Client next byte:
1001
Server next byte:
5001

Connection states

Each side keeps track of where it is in the process. This is called its connection state. You will see these names in netstat output and in firewall logs.

StateWhoMeaning
LISTENServerWaiting for a SYN on a port
SYN-SENTClientSent a SYN; waiting for a SYN-ACK
SYN-RECEIVEDServerReceived a SYN and sent a SYN-ACK; waiting for the final ACK
ESTABLISHEDBothHandshake done; data can flow both ways

Why three messages?

Each side has its own sequence numbers, and each side must know that the other has received its starting number. That needs a SYN and an ACK in each direction: four jobs. The server puts its SYN and its ACK in the same segment, so the four jobs fit into three messages.

Random ISNs matter too. If every connection started at 0, an old, delayed segment from a previous connection could be mistaken for new data. A random starting number also makes it much harder for an attacker to guess the numbers and inject fake segments.

If nothing is listening on the port, the server replies to the SYN with an RST (reset) instead of a SYN-ACK. If a firewall silently drops the SYN, the client gets no reply at all. It sends the SYN again a few times, then gives up with a timeout.

How to verify it

A Cisco router can test whether a TCP port answers: run telnet with an IP address and a port number, and the router tries to open a TCP connection to that port. The output below is based on Cisco documentation, not captured from a lab device.

Example output · based on Cisco documentation; exact format varies by platform and software version
R1#telnet 203.0.113.10 443
Trying 203.0.113.10, 443 ... Open
What to look for: Open means the handshake completed. The server replied with a SYN-ACK, and the router sent the final ACK. To leave, press Ctrl+Shift+6, then x, and enter disconnect.
Example output · based on Cisco documentation; exact format varies by platform and software version
R1#telnet 203.0.113.10 8080
Trying 203.0.113.10, 8080 ...
% Connection refused by remote host
What to look for: Connection refused means the server replied with an RST. The host is reachable, but nothing is listening on that port. A long wait that ends in Connection timed out usually means a firewall dropped the SYN, or the host is unreachable.

Check yourself

Predict · scenario 1

A client opens a connection with a SYN that has sequence number 7000. Which acknowledgement number is in the server's SYN-ACK?

Predict · scenario 2

You test a web server on port 8080. Your client sends a SYN and immediately gets an RST back. What does this most likely mean?

Predict · scenario 3

A server has received a SYN and sent its SYN-ACK, but the client's final ACK has not arrived yet. Which state is the server in?