What happens when you search for a kitten (on the Internet). Part 2: TCP or how not to lose the data on the road
This is the second part of the series where we talk about what happens when you use your browser to open a URL.
You might also want to check Part1: DNS or where to find the server and Part 3: TLS or how to protect your secrets.
Now that we have a destination address for our web application, you might think that it is a time to request a page we’re interested in. But no, not yet!
Before we can send a request for the page, we still need to do a couple of important things. In particular, we want to introduce ourselves to the web server and establish a reliable connection.
Why do we need to do this? We want to have a stable data exchange with our server application. The thing is, the information from a web server is not delivered in a single blob. For example, an average size of a web page is 3Mb. To deliver such a page the server will divide content into packages and send them one by one. The client (our browser) will catch the packages, combine them and present to the user.
Thus, for example an image below, is not sent as a whole file, rather it is sent as a collection of different pieces, or segments.
And here is a challenge: while on the road, many things can go wrong, some of the segments will get lost, corrupted, duplicated, or get out of order messing up the message to the client.
And the result might become unreadable and unrecognisable:
This problem is as old as the internet itself. To solve it we use Transmission Control Protocol or TCP for short. TCP guarantees that the segments received by the client are the same as they were sent by the server. To achieve this every segment is numbered. This helps the client to notice any missing segments and to put them into the correct order.
By the way the numbering is not started at 0. Rather we begin at some random value suggested by the server during the initial introduction.
The introduction process is called a three-way handshake. To start a connection three messages are exchanged during the process.
- Client sends synchronise (SYN) message to the server containing a random number (N)
- Server replies to the client with a message containing an acknowledgement (ACK) set to N + 1. Server also adds another SYN value equal to a random number (M)
- Client in turn sends an acknowledgement message with value set to M+1. Yes, introductions on the Internet are weird
With this we have created a reliable connection with the server and are ready to send and receive messages. Next, let’s talk about how to protect the data we send over the internet and avoid others from reading the messages we are exchanging with the server. You can find this in Part 3: TLS or how to protect your secrets.