TCP Working: 3-Way Handshake & Reliable Communication

1.What is TCP
TCP full form is Transmission Control Protocol. It first creates a connection (handshake), sends data packets in correct order, checks errors, and resends if packet is lost. That's why it's perfect for reliable things like web browsing, emails, file downloads, but it's a bit slow and uses more bandwidth.
Why TCP is Needed?
On internet, data packets can get lost, corrupt, or arrive in wrong order because IP only handles addressing, not reliability. TCP solves this with error-checking (using checksums), retransmitting lost packets, and flow control to prevent network overload. That's why apps like web browsing, email, file transfer use TCP where 100% correct data delivery is crucial.
2. Problems TCP is Designed to Solve
Data Loss
On internet, some packets can get lost on the way.
TCP ensures lost packets are resent.Data Order
Packets can take different routes and arrive in wrong order.
TCP arranges them in correct sequence.Duplicate Data
Sometimes same packet arrives twice.
TCP detects and removes duplicates.Reliability
TCP guarantees data reaches correctly and completely.
Sender doesn't consider data complete until receiver sends confirmation (ACK).Flow Control
TCP controls sender's speed based on how much data receiver can handle,
so receiver doesn't get overloaded.Congestion Control
When network has heavy traffic, TCP reduces sending speed,
so network doesn't crash.Error Detection
TCP uses checksum to detect if data is corrupt.
If corrupt, it's resent.Connection Management
TCP first establishes connection (3-way handshake),
then sends data, and properly closes connection when done.
3. What is the TCP 3-Way Handshake
TCP 3-Way Handshake is a process where client and server establish reliable connection before data transfer, so both sync on sequence numbers.
Step 1: Client Sends SYN
Client sends SYN (Synchronize) packet to server with its initial sequence number, like "Hello bro, create connection and this is my starting number."
Step 2: Server SYN-ACK Reply
Server receives SYN and sends SYN-ACK, SYN tells its sequence number and ACK confirms client's SYN, meaning "Yes bro, got your message and I'm ready too."
Step 3: Client ACK Confirm
Client sends final ACK acknowledging server's sequence, now both in established state and data transfer can start.

4. Step-by-Step Working of SYN, SYN-ACK, and ACK
TCP 3-Way Handshake's three steps sync client and server connection with sequence numbers for reliable data transfer.
Step 1: Sending SYN Packet
Client wanting connection sends first packet with SYN flag containing its initial sequence number (ISN, like 1000). It tells server "Bro start connection, my starting number is this" and client goes to SYN-SENT state.
Step 2: SYN-ACK Reply Arrives
Server receives SYN packet, sets ACK number (client's ISN +1, like 1001) and sends its SYN flag with own ISN (like 2000) in SYN-ACK packet. It says "Got your message (ACK), now my number is this (SYN)" and server waits in SYN-RECEIVED state.
Step 3: Final ACK Confirmation
Client on receiving SYN-ACK acknowledges server's ISN (server ISN +1, like 2001) by sending ACK packet without SYN. Now both client and server reach ESTABLISHED state, connection ready for data.
5. How Data Transfer Works in TCP
Data transfer in TCP happens through systematic and reliable process. Main goal is to deliver data correctly, completely, and in right order.
Step-by-Step
Connection Establishment
First 3-way handshake happens (SYN, SYN-ACK, ACK).
Data sending starts only after connection is made.Breaking Data into Packets (Segments)
Sender doesn't send big data directly, breaks it into small parts called TCP segments.Assigning Sequence Number
Each segment gets a sequence number
So receiver knows which data came first and which later.Sending Data
Sender sends these segments through network.Receiver Sends ACK
When receiver gets data, it sends ACK (Acknowledgement) to sender
Meaning: "I received this much data correctly."Re-transmission of Lost Packet
If any packet gets lost on way, receiver doesn't send ACK
Sender resends that packet after timeout.Arranging Data in Correct Order
If packets arrive in jumbled order,
TCP arranges them in right order using sequence numbers.Flow Control
Receiver tells how much data it can handle (window size)
Sender sends at that speed so receiver doesn't overload.Congestion Control
If network gets busy,
TCP reduces its sending speed so network doesn't jam.Error Checking
Each segment has checksum
If data gets corrupt, segment is rejected and requested again.Closing Connection
When complete data transfer finishes,
TCP properly closes connection (FIN, ACK process).
6. How TCP Ensures Reliability
Sender sends each data segment with sequence number, receiver sends ACK to confirm – if ACK doesn't arrive in timeout or duplicate ACKs come, lost segment is retransmitted. Corrupted data is detected with checksum and discarded, then resent.
Order Guarantee
Each byte gets unique sequence number, so out-of-order packets are stored in buffer by receiver and reordered in correct sequence before passing to application. This ensures data reaches exactly in sent order.
Correctness Mechanism
Checksum is calculated on each segment by sender and verified by both – mismatch causes packet drop, ACK sent for last correct byte. Flow control (sliding window) and congestion control prevent network overload so data stays correct.
7. How a TCP Connection is Closed
TCP connection is closed using 4-way handshake process, using FIN and ACK flags so no data loss and graceful termination happens.
Step 1: Client Sends FIN
When data transfer ends, client sends FIN (Finish) flag packet to server, meaning "Bro I'm stopping sending data". Client goes to FIN_WAIT_1 state.
Step 2: Server ACK Reply
Server receives FIN and sends ACK (acknowledging client's sequence) and goes to CLOSE_WAIT state. Now client->server data is stopped, but server can still send to client.
Step 3: Server Sends Its FIN
Server finishes its work and sends FIN to client, "Now I'm also stopping". Server waits in LAST_ACK state.
Step 4: Client Final ACK
Client ACKs server's FIN, goes to TIME_WAIT state (2-4 minutes to handle delayed packets), then fully closes. Server also goes to CLOSE state.