What is TCP?
Tags: programming, computerscience, beginners
Published: 2022-02-23 08:27:31


(my [newsletter](https://www.baseclass.io/newsletter/) subscribers received this first)

TCP (Transmission Control Protocol) is a protocol for machines to communicate over a network, and is the foundation on which the internet is built.

One of the most useful characteristics of TCP is that it is 'resilient', which means it can cope with an unreliable network without losing any data.

This is possible because the machine _receiving_ the data 'acknowledges' that it has received it. If the _sender_ doesn't get this acknowledgement, it knows to re-send the data.

![A server receiving, and acknowledging a cat picture](https://www.baseclass.io/newsletter/tcp-handshake/ack.png)

---

## How does it work? 

When we send data over a network, we normally group that data into 'packets':

![A client sending data to the server as 'packets'](https://www.baseclass.io/newsletter/tcp-handshake/packets.png)


At the beginning of each packet is the 'header'; a section of meta-data used to describe what is in the packet:

![A packet layout, showing the header followed by the data below](https://www.baseclass.io/newsletter/tcp-handshake/header.png)

The TCP header has a special block called the 'Sequence Number'.

**The Sequence Number is used to keep track of how many bytes have been sent and received by the two machines talking to each other.**

- The machine _sending_ the data sets the Sequence Number to the number of bytes that have been sent _in total_ during this conversation. It then sends the next block of data.
- The machine _receiving_ 'acknowledges' receipt of the data by **adding the number of bytes it has received to the Sequence Number, and passing it back to the sender**. It does this by setting a value in another part of the TCP header; the 'Acknowledgement Number'.

![A diagram showing the server receiving data and replying by increasing the Sequence Number](https://www.baseclass.io/newsletter/tcp-handshake/data_exchange.png)

This simple mechanism allows us to detect when data has gone missing; **if the _sender_ sends 20 bytes, but the _receiver_ only acknowledges 10, we know that some data has not been received**.

There are various mechanisms for dealing with this, but the simplest is for the _sender_ to simply re-send the data.

---

## Randomising the Sequence Number

For security reasons, we don't want the Sequence Number to be predictable. If it was, it would be possible to 'hijack' the communication.

So, even though the Sequence Numbers track how many bytes have been exchanged, they don't start from 0. Instead, both parties generate random starting values for their Sequence Number.

But if these numbers are random, how do both clients agree on them?

This is where the 'TCP Handshake' comes in...

---

## The TCP Handshake

When a connection is started, the first machine decides on its random 'Sequence Number' and sends it to the other party.

This transmission is called the `SYN`, because it is intended to '**SYN**chronise' the Sequence Number. 

The other side knows this is a `SYN` request, because we turn on a special bit in the packet header when sending it.

In this example, we've chosen `1234` as the random 'Sequence Number'.


![TCP Handshake part 1: The client sends a ransom Sequence Number](https://www.baseclass.io/newsletter/tcp-handshake/handshake-1.png)

---

The other party must now `ACK` or '**ACK**nowledge' the Sequence Number.

It does this by adding `1` to it, and passing it back in the 'Acknowledgement Number' section of the header (it also sets a special `ACK` bit in the header).

It now generates its own random Sequence Number and sends it back to the other machine.

In this example, it generated `5678` as its Sequence Number:

![TCP Handshake part 2: The server replies by incrementing 1 to the Sequence Number and sending its own random number](https://www.baseclass.io/newsletter/tcp-handshake/handshake-2.png)

---

Finally, the original party acknowledges the other machine's Sequence Number, also by adding 1 to it and sending it back in the 'Acknowledgement Number' section of the header:

![TCP Handshake part 3: The client acknowledges the server's Sequence Number](https://www.baseclass.io/newsletter/tcp-handshake/handshake-3.png)

The Handshake is now complete, and the clients can start communicating.

You might also see the TCP Handshake written as `SYN, SYN/ACK, ACK`, referring to the order the handshake packets are sent in.

---

### Want to know more?

Check out these links:


- [An in-depth explanation of TCP by IBM](https://www.ibm.com/docs/en/zos-basic-skills?topic=layer-transmission-control-protocol-tcp#znetwork_45__tcpheader)
- [Another summary of TCP](https://www.khanacademy.org/computing/computers-and-internet/xcae6f4a7ff015e7d:the-internet/xcae6f4a7ff015e7d:transporting-packets/a/transmission-control-protocol--tcp)
- [TCP Sequence Numbers Explained](https://madpackets.com/2018/04/25/tcp-sequence-and-acknowledgement-numbers-explained/)