Sub topic 7
Opening a connection: the three-way handshake
This chapter assumes you know the layered network model, what an IP address and a packet are, and the idea of ports. It also assumes you can read basic Python.
Why TCP needs a handshake
IP delivers packets on a best-effort basis. It does not tell either side whether the other is there. TCP builds a reliable byte stream on top of IP, so before any data moves, both ends must agree on three things:
- The other side exists and is willing to talk.
- Which sequence number each side will start counting from.
- Which options each side supports, such as the maximum segment size (MSS) and window scaling.
The exchange that settles this is the three-way handshake. It uses three segments and two header flags: SYN (synchronize) and ACK (acknowledge).
The three steps
Every TCP byte has a sequence number. Each side picks its own starting number, called the initial sequence number (ISN). Modern stacks choose it unpredictably, which makes it hard for an attacker to forge segments.
- SYN. The client sends a segment with SYN set and its ISN
x. It moves to the SYN-SENT state. - SYN-ACK. The server, which was in LISTEN, replies with SYN and ACK both set. It carries its own ISN
yand acknowledgesx + 1. It moves to SYN-RECEIVED. - ACK. The client acknowledges
y + 1. Both sides are now ESTABLISHED.
The SYN flag consumes one sequence number even though it carries no data. That is why the acknowledgment is x + 1, not x.
The two ISNs are independent. Each direction of the connection has its own sequence space, so each side must prove it received the other's number. Three segments are the minimum that does this: the server's SYN and its acknowledgment of the client's SYN travel together in step 2.
What the handshake costs
The client cannot send data until it has seen the SYN-ACK, so opening a connection costs one round-trip time (RTT) before the first request goes out. On a 100 ms path, that is 100 ms of waiting. Secure protocols add more round trips on top of this.
The handshake in code
You do not build SYN segments yourself. The operating system does it, and the sockets API exposes the result.
- On the server,
listen()puts the socket in LISTEN. The kernel completes handshakes with clients and queues the finished connections. accept()takes one finished connection from that queue. It returns a new socket for that client.- On the client,
connect()sends the SYN and returns once the handshake completes or fails.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(("127.0.0.1", 9000))
srv.listen()
conn, addr = srv.accept()
with conn:
print("handshake done with", addr)Run this, then connect from another terminal with python -c "import socket; socket.create_connection(('127.0.0.1', 9000))". The server prints the client's address and ephemeral port.
Common errors and what they mean
| Symptom | Likely cause |
|---|---|
| `ConnectionRefusedError` right away | The host answered with a reset (RST) because no process listens on that port. |
| `connect()` hangs, then times out | The SYN was dropped, often by a firewall, or the host is down. Nothing replied. |
| Server stuck with many SYN-RECEIVED entries | A SYN flood: clients send SYNs and never complete step 3. |
| `Address already in use` on restart | The old socket is still in a closing state. `SO_REUSEADDR` allows rebinding. |
Check your understanding
Exercise
A client sends SYN with sequence number 1000. The server chooses ISN 5000. What are the sequence and acknowledgment numbers in the SYN-ACK, and what acknowledgment number does the client send in the final ACK?
Show answer
The SYN-ACK has seq=5000 and ack=1001, because the SYN consumes one sequence number. The client's final ACK has ack=5001.
Exercise
A connect() call fails instantly with "connection refused." A second call to a different host hangs for about a minute before failing. What does each result tell you about the SYN that was sent?
Show answer
The first host received the SYN and replied with RST, so it is reachable but nothing listens on that port. The second host never replied, so the SYN or its reply was lost, for example dropped by a firewall, or the host is unreachable.