Skip to main content

Command Palette

Search for a command to run...

TCP (Transmission Control Protocol)

Published
•3 min read•View as Markdown
TCP (Transmission Control Protocol)
S

Software developer writing to think clearly about code, systems, and real-world tradeoffs.

What happens if data is sent without rules?

When data is sent over the internet without any rules:

  • Data may not reach the destination

  • Data may arrive in the wrong order

  • Some data may be lost

  • Same data may arrive more than once

  • Sender does not know if data was received or not

Because of these problems, communication becomes unreliable.


What is TCP and why is it needed?

TCP (Transmission Control Protocol) is a communication protocol that makes data transfer reliable.

TCP is needed because it:

  • Ensures data is delivered correctly

  • Ensures data arrives in the correct order

  • Detects missing or duplicate data

  • Confirms data delivery

TCP is used in applications where accuracy is important, such as:

  • Web browsing

  • Email

  • File downloads

  • Online logins


Problems TCP is designed to solve

TCP is designed to solve the following problems:

  1. Packet loss

  2. Out-of-order delivery

  3. Duplicate packets

  4. No delivery confirmation

TCP solves these problems using acknowledgements, sequence numbers, and retransmission.


What is the TCP 3-Way Handshake?

Before data transfer starts, TCP first creates a connection between the client and the server.
This connection setup process is called the 3-Way Handshake.

It ensures that:

  • Both sides are ready to communicate

  • Both sides agree on starting data numbers

TCP 3-Way Handshake Diagram

https://static.afteracademy.com/images/what-is-a-tcp-3-way-handshake-process-three-way-handshaking-terminating-connection-6ea4a4c72d165361.jpg

https://networkwalks.com/wp-content/uploads/2020/10/TCP-three-way-handshake-process-1.png

https://static.afteracademy.com/images/what-is-a-tcp-3-way-handshake-process-three-way-handshaking-establishing-connection-6a724e77ba96e241.jpg


TCP 3-Way Handshake (Step-by-Step)

Step 1: SYN

Client sends a SYN (synchronize) message to the server.
Meaning: “I want to start a connection.”

Step 2: SYN-ACK

Server replies with SYN-ACK.
Meaning: “I received your request and I am ready.”

Step 3: ACK

Client sends ACK.
Meaning: “Connection established.”


How data transfer works in TCP

Once the connection is established:

  • Data is divided into small segments

  • Each segment has a sequence number

  • Receiver sends ACK after receiving data

  • Sender sends the next segment

TCP Data Transfer Diagram

https://static.wixstatic.com/media/a2409e_aef337e8e53946f383f37bad39fe1028~mv2.jpg/v1/fill/w_566%2Ch_480%2Cal_c%2Clg_1%2Cq_80/a2409e_aef337e8e53946f383f37bad39fe1028~mv2.jpg

https://media.geeksforgeeks.org/wp-content/uploads/20250513105835416942/tcp.webp

https://www.researchgate.net/publication/331853081/figure/fig1/AS%3A738951123972098%401553191085167/TCP-sliding-window-mechanism.png


How TCP ensures reliability, order, and correctness

Reliability

Lost data is retransmitted.

Order

Sequence numbers arrange data correctly.

Correctness

Corrupted data is discarded and resent.


How TCP handles packet loss

If a packet is lost:

  • Receiver detects missing sequence number

  • ACK is not sent

  • Sender retransmits the lost packet

Packet Loss and Retransmission Diagram

https://www.researchgate.net/publication/382092781/figure/fig1/AS%3A11431281259508197%401720533616113/TCP-packet-loss-detection-and-retransmission-triggered-by-a-timeout-or-by-three-duplicate.ppm

https://assets.extrahop.com/images/infographics/TCP-RTO-Retransmission-Timeout-Diagram.jpg

https://www.researchgate.net/publication/251345982/figure/fig9/AS%3A668291504799754%401536344519230/Example-of-TLTCP-missing-a-packet-loss-in-the-obsolete-data.png


How a TCP connection is closed

When communication is finished, TCP closes the connection safely.

Connection Termination Steps

  1. One side sends FIN

  2. Other side sends ACK

  3. Other side sends FIN

  4. Final ACK is sent

TCP Connection Close Diagram

https://upload.wikimedia.org/wikipedia/commons/8/8a/TCP_connection_Termination.png

https://media.geeksforgeeks.org/wp-content/uploads/20220406111342/TheFourWayHandshakeProcess.jpg

https://www.ibm.com/support/pages/system/files/inline-images/Flow%20chart%20TCP%20connection_0.jpg


TCP Connection Lifecycle

  1. Connection establishment

  2. Data transfer

  3. Error handling and retransmission

  4. Connection termination


Summary

  • TCP provides reliable communication

  • It ensures ordered and correct data delivery

  • Uses 3-way handshake to start connection

  • Uses sequence numbers and ACKs for data transfer

  • Retransmits lost packets

  • Closes connection safely using FIN and ACK

More from this blog

web development

15 posts