codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🌐

Computer Networks

26 chapters

1Network Topologies & LAN/WAN2Network Devices (Hub, Switch, Router)3OSI Reference Model4OSI Model & TCP/IP Suite5TCP/IP Protocol Suite6Switching Techniques (Circuit, Packet)7Data Link: Framing & Error Detection8Error Correction (Hamming Code)9Flow Control (Stop-and-Wait, Sliding Window)10MAC: CSMA/CD & CSMA/CA11Network Layer & Routing12IP Addressing (IPv4, IPv6)13Subnetting & CIDR14Routing Algorithms (Distance Vector, Link State)15ARP, ICMP, and NAT16DHCP Protocol17Transport Layer Services18Transport Layer: UDP19Transport Layer: TCP & 3-Way Handshake20TCP Congestion Control21Application Layer: DNS & HTTP22Application Layer: SMTP & FTP23Socket Programming Basics24Wireless Networks & Wi-Fi Standards25VLANs & Spanning Tree Protocol26Network Security & Cryptography
SubjectsComputer Networks

Transport Layer: TCP & 3-Way Handshake

Updated 2026-05-03
4 min read

Transport Layer: TCP & 3-Way Handshake

While UDP is fast and unreliable, the vast majority of Internet traffic (Web browsing, Email, File Transfers) requires absolute perfection. If a single byte is lost while downloading a massive executable file or a bank statement, the entire file is corrupted and useless.

To guarantee perfect delivery over an inherently unreliable network (the Internet), the Transmission Control Protocol (TCP) is used.

1. Features of TCP

TCP provides a massive amount of functionality over UDP:

  1. Connection-Oriented: Before any data is sent, TCP establishes a strict, stateful connection between the client and the server.
  2. Reliable Delivery: TCP guarantees that every single byte sent will arrive. If a router drops a packet, TCP detects the loss and automatically retransmits it.
  3. In-Order Delivery: IP packets can take different routes across the globe and arrive out of order. TCP assigns a Sequence Number to every packet and reassembles them in the exact correct order before passing them to the application.
  4. Flow Control: If a supercomputer is sending data to a 10-year-old smartphone, the smartphone's memory buffer might overflow. TCP allows the receiver to tell the sender exactly how much data it can handle at any given moment (the Receive Window), preventing the sender from overwhelming the receiver.

2. The TCP Header

Because of all these features, the TCP header is large (minimum 20 bytes). Key fields include:

  • Source & Destination Port: Identifies the specific applications.
  • Sequence Number: Marks the byte offset of the data in this packet relative to the entire stream.
  • Acknowledgment Number: Tells the sender which bytes the receiver has successfully received so far.
  • Flags (Control Bits): 6 specific bits used to manage the connection state. The most important are SYN (Synchronize), ACK (Acknowledgment), FIN (Finish), and RST (Reset).

3. The 3-Way Handshake

Before a client and server can exchange a single byte of application data (like an HTTP request), they must establish a TCP connection. This is done via the 3-Way Handshake.

The purpose of the handshake is to synchronize the Sequence Numbers and exchange the Initial Window Sizes between both machines.

Step 1: SYN (Client $\to$ Server)

The client wants to establish a connection. It generates a random Initial Sequence Number (e.g., $X = 1000$) and sends a TCP segment with the SYN flag set to 1.

  • Meaning: "Hello server, I want to connect. My starting sequence number is 1000."

Step 2: SYN-ACK (Server $\to$ Client)

The server receives the request. It allocates memory buffers for the connection. It generates its own random Initial Sequence Number (e.g., $Y = 5000$). It sends back a segment with both the SYN and ACK flags set to 1. The Acknowledgment number is set to $X + 1$ (1001).

  • Meaning: "Hello client, I acknowledge your sequence 1000 (I am now expecting byte 1001). I also want to connect. My starting sequence number is 5000."

Step 3: ACK (Client $\to$ Server)

The client receives the SYN-ACK. It allocates its memory buffers. It sends a final segment with the ACK flag set to 1, acknowledging the server's sequence number by setting the ACK field to $Y + 1$ (5001).

  • Meaning: "I acknowledge your sequence 5000 (I am now expecting byte 5001). We are officially connected."

Once the 3-Way Handshake is complete, the connection is ESTABLISHED, and the client can finally send the HTTP GET request.

4. Connection Termination (4-Way Teardown)

When the application is done, the connection must be closed gracefully to ensure no data in transit is lost.

  1. The Client sends a FIN segment (I have no more data to send).
  2. The Server replies with an ACK (I acknowledge you are done). The server might still have data to send back, so the connection remains half-open.
  3. When the Server is finished sending its final data, it sends its own FIN segment.
  4. The Client replies with an ACK. The connection is officially closed.


PreviousTransport Layer: UDPNextTCP Congestion Control

Recommended Gear

Transport Layer: UDPTCP Congestion Control