TCP Working: 3-Way Handshake & Reliable Communication
3-Way Handshake &

TCP (Transmission Control Protocol) is a core protocol of the Internet that ensures reliable, ordered, and error-free data transfer between devices. It establishes a connection before sending data and manages the delivery using acknowledgments, sequence numbers, and retransmissions.
What is TCP and why it is needed
i) What is TCP
Transmission Control Protocol (TCP) is the standard way devices communicate and exchange data over a network. It doesn't just send data it establishes and maintains a connection between the sender and receiver during the transfer, ensuring information flows smoothly.
ii) Why is it needed?
TCP is crucial because it sets the rules and standards for reliable communication between programs over a network. Without it, protocols like HTTP and HTTPS wouldn't work correctly, and web access, file transfers, or email delivery could become unreliable. It ensures data arrives complete, in order, and error-free, making modern networking dependable.
Example:
When you load a website, TCP makes sure all the content—text, images, and videos—arrives intact and in the correct order, so the page displays properly on your browser.
Problems TCP is designed to solve
TCP, or Transmission Control Protocol, is the internet’s way of ensuring data travels safely from one device to another. It breaks messages into packets, checks that they all arrive, and puts them back in the right order. TCP resends lost packets, fixes corrupted ones, and ignores duplicates. Without it, web pages, videos, and files could arrive scrambled or incomplete. Basically, TCP is like a super-organized postman for the internet.
Stands for Transmission Control Protocol
Ensures reliable delivery of data over networks
Breaks data into packets and tracks them
Resends lost packets and fixes corrupted ones
Puts packets back in the correct order
Prevents duplicate data
Used by HTTP, HTTPS, email, and file transfers
Example:
Imagine sending a puzzle to a friend through the mail. Some pieces get lost, some arrive twice, and others are in the wrong order. TCP is the magical postman who chases missing pieces, throws out duplicates, and arranges them perfectly so your friend can enjoy the full puzzle.

What is the TCP 3-Way Handshake
The TCP 3-way handshake is how a client and server establish a reliable connection before exchanging actual data.
Step 1 – SYN: The client starts by sending a SYN (synchronize) packet to the server. This packet says, “Hey, I want to talk, and here’s the sequence number I’ll start with.”
Step 2 – SYN + ACK: The server replies with a SYN-ACK packet. The ACK confirms it received the client’s request, and the SYN tells the client the server’s starting sequence number.
Step 3 – ACK: The client responds with an ACK to acknowledge the server’s sequence number. At this point, both sides know the connection is established, and they can start sending actual data reliably
Example :
You reach out your hand (SYN) → “Hi, can we start?”
The other person shakes back and says, “Sure, here’s my starting point” (SYN + ACK)
You nod and return the handshake (ACK) → now the conversation begins!
It’s like a handshake before a conversation: the client reaches out (SYN), the server shakes back and introduces itself (SYN+ACK), and the client nods to confirm (ACK). Now both are ready to communicate.

Step-by-step working of SYN, SYN-ACK, and ACK

Step 1: SYN (Synchronize)
In the first step, the client wants to establish a connection with a server, so it sends a segment with SYN(Synchronize Sequence Number) which informs the server that the client is likely to start communication and with what sequence number it starts segments with
The client wants to start communication with the server.
It sends a SYN packet to the server, which contains a starting sequence number (Seq).
This tells the server: “I want to talk, and here’s the number I’ll start sending data with.”
Step 2: SYN-ACK (Synchronize + Acknowledge)
Server responds to the client request with SYN-ACK signal bits set. Acknowledgement(ACK) signifies the response of the segment it received and SYN signifies with what sequence number it is likely to start the segments with
The server receives the SYN request.
It replies with a SYN-ACK packet, which has two purposes:
ACK: Confirms it received the client’s SYN (acknowledges the client’s sequence number + 1).
SYN: Provides the server’s own starting sequence number for the communication.
This tells the client: “I got your request, and here’s where I’ll start sending my data from.”
Step 3: ACK (Acknowledge)
In the final part client acknowledges the response of the server and they both establish a reliable connection with which they will start the actual data transfer.
The client receives the server’s SYN-ACK.
It sends back an ACK packet, acknowledging the server’s sequence number (server Seq + 1).
After this, the connection is established, and both sides can start exchanging actual data.a reliably.
How data transfer works in TCP

TCP ensures that data sent over a network is reliable, organized, and efficient. It first breaks large messages into smaller packets and passes them to the IP (Internet Protocol) layer, which routes them toward the destination. Packets may travel along different paths if certain routes are congested or unavailable, improving network performance. TCP also monitors delivery, detects lost or corrupted packets, and reassembles them in the correct order, so the receiving device gets the original message intact.
TCP splits data into smaller packets for transmission
Works with IP to route packets efficiently
Different paths are used to avoid congestion
Tracks and reorders packets at the destination
Detects and resends lost or corrupted packets
By dividing communication into layers and managing packets carefully, TCP makes data transfer reliable, standardized, and efficient. It's like a smart courier service that ensures every parcel reaches its destination intact, even if it has to take multiple routes, fix broken pieces, or reorder the parcels perfectly.
How TCP ensures reliability, order, and correctness
Reliability : in TCP is achieved through sequence numbers and acknowledgments (ACKs). Each byte of data gets a unique sequence number, allowing the receiver to confirm exactly what has been received. The receiver sends ACKs back to the sender, and if an ACK is not received within a certain time, TCP retransmits the missing data, ensuring no information is lost during transmission.
Order : is maintained using these sequence numbers at the receiving end. Even if packets arrive out of order because they traveled through different network paths, the receiver uses the sequence numbers to reassemble them in the correct order before delivering the data to the application.
Correctness : is ensured using checksums, which let TCP detect any data corruption during transmission. If a received segment fails the checksum test, it is discarded and retransmitted. Additionally, TCP uses sequence numbers to detect duplicates and avoid processing the same data twice, ensuring the final data received is accurate and error-free.
How a TCP connection is closed
A TCP connection is closed using a graceful four-step termination process to ensure that both devices finish sending and receiving data correctly. First, the device that wants to end the communication sends a FIN (Finish) packet, indicating it has no more data to transmit. The receiving device replies with an ACK (Acknowledgment) to confirm receipt of the FIN, while it may still send remaining data. Once the receiver is done transmitting, it sends its own FIN packet. Finally, the original sender responds with an ACK, after which the connection is completely closed.
Sequence:
FIN → ACK → FIN → ACK
1. FIN (from Initiator):
The device that wants to close the connection sends a FIN (Finish) packet. This means, “I have finished sending data, but I can still receive data from you.”
2. ACK (from Receiver):
The receiving device responds with an ACK (Acknowledgment) to confirm it received the FIN. At this point, the connection is half-closed—the receiver can still send remaining data.
3. FIN (from Receiver):
After the receiver finishes sending its remaining data, it sends its own FIN packet, indicating it is now done transmitting.
4. ACK (from Initiator):
The original sender replies with a final ACK, confirming the receiver’s FIN. The TCP connection is now fully closed on both sides.
Summary
TCP starts communication using a 3-way handshake (SYN → SYN-ACK → ACK) to establish a reliable connection between two devices. Once connected, TCP ensures reliable data transfer by breaking data into packets, using sequence numbers, acknowledgments (ACKs), retransmissions, and checksums to guarantee correct order and error-free delivery.




