Course contents

Unit 7: TCP, UDP and PortsLesson 7.1.6 (6 of 23 in this unit)55 of 104 in the Network Fundamentals course

The three-way handshake

Why can a server answer a ping but still refuse a web connection? Let's follow what TCP checks before the browser can start its application conversation.

Beginner · 6 min read

Covers SYN / SYN-ACK / ACK, Initial sequence numbers and Connection states.

Technical definition

TCP three-way handshake is the SYN, SYN-ACK and ACK exchange used in ordinary TCP connection establishment. It synchronises the endpoints' initial sequence numbers and establishes connection state. This lesson follows a basic handshake without TCP Fast Open.

Checking the line before talking

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. In the basic exchange shown here, TCP checks readiness before ordinary application data. This short exchange is the three-way handshake.

SYN, SYN-ACK, ACK

Keep two checks separate. IP reachability gets the SYN to the server; a listening TCP service and the relevant host/firewall rules determine whether it can accept this connection. The server does not use the browser's source port as its own listening port. In this example it listens on 443 and sends its reply back to the client's temporary port 52000.

A completed TCP handshake proves that TCP connection setup succeeded; it does not prove that TLS, login or the web application will succeed. For a typical HTTPS connection, TLS negotiation follows TCP setup. TLS is the security protocol that authenticates and encrypts the application conversation. A failure there is a different troubleshooting stage.

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 client application starts a TCP connection to a listening service on the server. Arrows show SYN, SYN-ACK and ACK messages in order; they are not three cables. This simulation sends no real traffic.

Following the sequence numbers through the handshake

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

Device key: Client (laptop)

Arrows run from each message's sender to its receiver. Rows show message order, not separate cables or elapsed time to scale. Port numbers identify services or conversations, not physical switch sockets.

Reading the addresses

IP labels identify interfaces or destinations. Subnet labels identify networks; /n is the prefix length. A router ID (RID) identifies a routing process and need not be a reachable interface.

An IP address alone is not a URL. A web URL includes a scheme such as https://; an address on a router or printer does not imply a website.

  • Client: 192.168.1.20:52000
  • Web server: 203.0.113.10:443

LISTEN to ESTABLISHED: the 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.

Diagram
LISTEN to ESTABLISHED: the connection states
LISTEN to ESTABLISHED: the connection states
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
Read each row across: the left box names the item, the others describe it.

Why four jobs fit into 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.

Testing a TCP port with telnet from a router

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, or a firewall in front of it, replied with an RST. Usually 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.

That difference between “refused” and “timed out” is one of the quickest clues in troubleshooting. Refused points at the server or its application; a timeout points at the path or a firewall in between.

Test yourself on the handshake

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?

Technical references

RFC 9293: base TCP header, connection state, sequence space and delivery.