Learn more about Russian war crimes in Ukraine.

What is the TCP three-way handshake?

There are three blocks here, corresponding to three IP packets. This is the “TCP three-way handshake”. The goal of this process is to establish a connection.

Connection establishment is like a legal contract. Each party must agree to the contract, and have a copy of the other party’s agreement to the contract. The contract content could be thought of as and shall transmit two byte streams between each other. shall send a byte stream from port 56742 to port 12345 on shall send a byte stream from port 12345 to port 56742 on

The first packet is the client requesting a connection. But the connection contract can’t be achieved with a single “open connection” packet. TCP works over IP, where there is no guaranteed delivery; thus the client cannot know that the server received the “open connection” packet. Also, the server might choose to deny the connection even if it receives the request!

The client wishes to know whether the the server has accepted the connection. This acceptance is called “acknowledgement” in TCP, and this is the second packet in the handshake: the server acknowledges the connection.

Doesn’t it seem like two packets are enough to establish the connection? A legal contract could work this way. But two packets are not enough, because the “connection contract” actually consists of a couple more facts:

The first byte in the byte stream from shall have number 3112279261. The first byte in the byte stream from shall have number 3504942089.

These numbers 3112279261 and 3504942089 are “initial sequence numbers”. You can see them in the tcpdump output. An initial sequence number is chosen at random by its source host, e.g. chose 3112279261 at random and chose 3504942089 at random. Thus the full contract is not written by the client; it is only known after the server responds. This means the server now wants a response from the client, acknowledging the server’s initial sequence number. This is the third packet in the three-way handshake.

(Why do we have these random initial sequence numbers? I’ll cover that some other time.)

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2018. This post is not associated with my employer. Found an error? Edit this page.