TCP - Transmission Control Protocol
TCP is a connection oriented, reliable transmission protocol. It has the following features:
- Reliable - when transferring data across the internet, packets may be dropped due to lost connection. TCP uses acknowledgement to ensure that data is retransmitted even if it is dropped
- Connection Oriented - depending on what data is being sent, the ordering is quite important. TCP uses sequence numbers to keep track of the order in which data is being sent.
- Flow/Congestion Control - TCP uses mechanisms to ensure that there’s no congestion when data is being transmitted. Sending too much or too little data can cause reliability issues:
- Too much data can lead to packet loss which triggers constant retransmission of data(this is quite inefficient)
- Too little data would mean that less data is sent(which is also quite inefficient)
This is what a TCP packet looks like. It contains the following data:
- 1st row:
- source/destination ports(16 bit)- port number to send/receive data
- 2nd row:
- Sequence number(32 bit) - to keep track of the order of data
- 3rd row:
- Acknowledgement Number(32 bit) - to keep track of what data has been received
- 4th row:
- Data offset: Specifies the size of the header so the computer knows what position to start reading off to obtain data
- Flags: these can be thought of as options for how the protocol works:
- ACK - indicates that the packet contains an acknowledgement
- RST - reset the connection
- SYN - start a connection
- FIN - end a connection
- 5th row:
- Checksum - a value that is checked by the receiver to ensure that the header is not corrupted
- 6th row:
- Data sent by the application
When a computer wants to send data using TCP, it needs to start a connection. It does this using what is called a 3 way handshake:
[1] The initiating connection(client) first sends a SYN packet with an initial packet number
[2] The receiving end(server) sends a packet with the SYN and ACK flags set where the acknowledgement number of this packet is the sequence number of the packet sent by the client. The server sets its own sequence number
[3] The client receives this packet and sends a new packet with the ACK flag set and the acknowledgement number set as the initial sequence number sent by the server
After the 3rd packet, the client and server begin transferring data.TCP also has a handshake to tear down a connection but this isn’t relevant for now.