TCP Demystified: How Data Travels Securely Across Networks

In simple terms, TCP (Transmission Control Protocol) is a protocol use to transfer data from a computer to another computer. Generally, one of the computers is “server/sender” and another one is “receiver”. TCP ensure that data transfer should be reliable and error free.

TCP’s features:

  1. Reliability: It ensures that receiver receives the data completely and accurately.

  2. Error Checking: If there is an error in data transfer, TCP will detect this and retransmit the data.

  3. Sequencing: TCP sends the data in form of packets (data is divided into various small parts), and give a sequence number to each, so that receiver get the data in a correct sequence.

  4. Flow Control: Data flow is also control by TCP, to prevent the overloading in receiver.

  5. Connection-Oriented: A connection is established in the starting of data transfer and is closed after the transfer is complete.

For better understanding, break down the data transfer process of TCP in various stages.

Establish a connection:

First thing is to establish a connection between two computers(3-way-Handshake).

Let’s understand this with a flow diagram:

Explanation:

Step 1: SYN (Synchronize):

As we know that data is transferred in packets, so server sends the SYN flag to establish a connection. In this packet server also initialize its sequence number which it will use during the data-packet transfer.

Step 2: SYN-ACK (synchronize and acknowledge):

Now receiver will send SYN-ACK flag with their sequence number in the reply of server’s SYN-packet. This packet is only to acknowledge that receiver is ready for the connection.

Step 3: ACK(Acknowledge):

In response to the SYN-ACK flag from the receiver, server send ACK flag, now connection is established and ready to transfer data.

Acknowledge Number Concept:

It plays an important role in TCP. Acknowledge number is send by receiver and it represents the next expecting sequence number from server. The next expected sequence number is for the next packet that the server needs to send now.

ACK number = sequence number of next expected packet

Let’s understand this with the help of flow diagram:

Server: let server want to send four packets named as p1, p2, p3, p4 (sequence number: 1,2,3,4).

Receiver: it receives p1(packet) and in response send ACK-2, this means that receiver receives p1 and expecting p2(packet) that’s why it uses ACK-2.

Data Transfer:

Flow control:

Main purpose of flow control is to prevent receiver from overloading. It maintains the balance between receiver and server, it ensures that server will send the packets(data) according to the processing capacity of receiver. Otherwise, excessive number of packets will overflow the buffer of receiver and it will lead to loss of packets(data). Ultimately it leads to latency.

Buffer:

Buffer is simply the size of temporary space of receiver in which it will store data before processing it. Before sending the data there is an option that receiver gives information about the buffer capacity, so that server will send accordingly to maintain the proper flow of data without corruption and loss of packet.

Sliding Window:

Let’s understand with the flow diagram:

Explanation:

Server: let server wants to send multiple packets, but initially it can send only four packets [ p1, p2, p3, p4] because receiver has buffer size of four packets.

Receiver: when receiver receive these four packets, it starts processing as the p1 packet processed it will send the acknowledgement (ACK-5) to server. ACK-5 represents the next expected packet which receiver now expects.

Window Slide: In the beginning, server’s window consists of [p1, p2, p3, p4]. As soon as it receives the ACK-5, its window slides to [p2, p3, p4, p5] and it send the p5 packet to receiver and so on.

Error Handling:

When data is divided into small packets, it attaches a checksum value for receiver.

Checksum calculation:

Server actually process data byte-by-byte and do mathematical sum on data (01010101) and attach its value with packet which helps in checking the validation of data.

Checksum verification:

When the packet receives by receiver, receiver also apply mathematical sum and check if it is similar to the value (attached checksum) which came from server. If it matches than receiver sends the ACK for next packet, and if not than receiver discard the packet, and server retransmit the packet with that specific sequence number.

Congestion control:

Main purpose of this is to use network resources efficiently and to fulfill that, TCP do the followings:

Slow start:

Initially the server starts with small amount of data Because there can be a lot of traffic or connected devices, and the signal might also be weak, or there could be noise in the network and eventually it will cause packet corrupt or loss. As soon as it gets the acknowledgement from receiver, it will increase the data amount (window size increase) so that load increase gradually and this prevent overloading because overloading leads to data corruption, data loss and which we don’t want to do that.

Congestion avoidance:

To avoid the congestion TCP, use AIMD mechanism (Additive Increase Multiplicative Decrease). In this mechanism server/sender increase the data amount slowly and if it detects the data loss. It will decrease the transmission rate suddenly.

Fast retransmit:

When sender gets any indication of data loss. It will activate the fast transmit mechanism and resend the loss data(packet) as soon as possible without waiting of the time out signal. In this way TCP avoid congestion and delay.

Fast recovery:

After Fast retransmit, TCP activate Fast recovery in which it reduces the window size, but it never reaches to full congestion avoidance mode. In this way TCP avoid congestion and give better user experience.

Connection Termination (4-way Handshake):

Now let’s understand with flow diagram:

First step: Now server initiate this termination process by sending FIN flag when server has completed its data transfer task.

Second step: In response to this receiver send ACK.

Third step: Now receiver send FIN flag to initiate the connection termination.

Fourth step: Now sender acknowledge the receiver by sending ACK and close its connection.