Imagine this: You are in a classroom and want to ask your friend to give you a pencil. Your friend is sitting on the last bench(far from you), and there is a lot of noise in the room. You called your friend and asked, "Hey, give me a pencil". He then gave you a "Pen". What that means is he could not hear you properly due to the nose. Maybe he just heard the word "Pen".
This is what actually happens with Machines in the real world. While transferring data from one computer to other there can be a data loss. To solve this, we decided that we would have a set of rules that we would follow during our communications.
Let's get back to the classroom analogy. Now Before asking your friend for a Pencil you asked him, "Hey, can you hear me?" Then he must reply with "Yes, I can hear you". Then you proceeded to ask him a "Pencil". And after properly hearing you, he gave you the pencil.
Similarly, when computers try to communicate over the internet, they follow a set of Protocols(Agreed Rules). The rules are TCP/IP, UDP, etc.
In this blog, we will discuss about the TCP
What is TCP
TCP (Transmission Control Protocol) is a protocol that allows devices to communicate reliably over a network. It ensures that data reaches the destination correctly and in the right order, even if parts of the network are slow or unreliable.
Some Problems that TCP solves:
- Reliability - Even if the network is slow, it will ensure that the data reaches its destination.
- Ordering - The sequence of the data packet from the sender must be the same as that of the receiver.
- Loss Detection - It has the capability to detect the loss of a packet during transmission.
- Retransmission - After detecting the loss of a packet, It can initiate a retransmission.
- Flow Control - The rate at which the data is sent from the sender must be the same as the data is received by the receiver.
- Congestion Control - It reliably controls the traffic in the bandwidth.
Before two computers can exchange data, they must establish a connection. This is done using the famous 3-Way handshake.
The 3-Way Handshake
TCP is connection-oriented, meaning a connection must be established before any data is sent. This is done using a three-way handshake:
- SYN (Synchronise): The sender sends a SYN segment to the receiver to request a connection.
- SYN-ACK (Synchronise-Acknowledge): The receiver responds with a SYN-ACK segment, acknowledging the request and agreeing to the connection.
- ACK (Acknowledge): The sender replies with an ACK, confirming the connection is established.
Let's imagine the classroom analogy again. You need a Pencil and for that you need to ask your friend who is sitting far from you. There is a lot of noise in the classroom. So what you actually do is:-
- You - "Hey buddy, can you hear me?" (SYN)
- Friend - "Yes, I can hear you! Can you hear me?" (SYN-ACK)
- You - "Yes, I can hear you too!" (ACK)
[Connection established - now data transfer begins]
- You - "Can you give me a Pencil?" (Data transfer)
- Friend - "Here's your pencil!" (Data + ACK)
This process will ensure that both the Sender and the Receiver have established a connection and are ready to transfer the data.
How the 3-Way Handshake works
STEP 1: SYN (Synchronisation)
Your computer sends a special packet to the server with a SYN flag set. This packet says, "Hey, I want to connect with you. Here is my sequence number".
A sequence number is nothing but a random number(Let's say 1000) where the computer will start counting its data packet.
STEP 2: SYN-ACK (Synchronisation - Acknowledge)
If the server is listening and is willing to connect. It will send a data packet back to the sender with a SYN and an ACK flag set. This packet says,s "Hey, I received your packet, and I am willing to connect. Here is my sequence number,"
STEP 3: ACK(Acknowledge)
Finally, the sender sends back the acknowledgement packet to confirm that it received the receiver's message. This packet says, "Perfect, I acknowledge your sequence number. Let's start exchanging information."
Data Transmission by TCP
Segmenting
When a sender sends data to the receiver, it breaks the data into smaller packets called segments. Each segment contains a HEADER that contains the information like sequence number, Port number and flags.
Routing
Once the segmentation is done, the IP is responsible for transferring the segments from the sender to the receiver. There is now no role for TCP here.
Reassembly
The segments that are received at the receiver end may be out of order because each segment may have taken a different path. The TCP at the receiver side receives all the segments in order and creates the original message by using the sequence number.
Acknowledgements
After the receiver receives each segment, it sends an acknowledgement back. This tells the senders that if there is a packet loss or something.
Retransmission
If there is a packet loss, the ACK is received by the sender. Then the sender will assume that there is a loss of a packet, and retransmission will occur. This ensures no data loss.
Flow control
TCP prevents the sender from sending too much data too quickly for the receiver to handle, using a sliding window mechanism.
Error Control
TCP checks for corrupted segments using checksums and requests retransmission if needed.
The 4-Way handshake
When communication is complete, the connection needs to be closed properly. TCP uses a 4-way handshake for termination.
STEP 1: FIN(Finish)
After the data transmission is done. The sender sends a data packet with a flag FIN set. This packet says, "I am done sending the data. Let's close the connection".
STEP 2: ACK(Acknowledgement)
The server receives the packet and then sends a packet with a flag ACK set. This packet says, "I got your packet, Sure lets close the connection"
STEP 3:FIN
The receiver will then send a packet with a flag FIN set. This packet says "Okay, I am closing the connection".
STEP 4: ACK
The Sender will get the packet and will send another packet with a flag ACK set. This packet says, "Well then, Bye Bye"
Want to learn more about networking? Check out my previous blogs on DNS Resolution and How the Internet Actually Works.




Top comments (0)