Tags

  • AWS (8)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • Database (1)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (16)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • Miscellaneous (1)
  • MySQL (21)
  • Neovim (11)
  • Network (72)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (4)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    Transmission Control Protocol

    Published Sep 22, 2021 [  Network  ]


    πŸ”§ How TCP Works

    1. Connection Establishment: The 3-Way Handshake

    To start a TCP session, both sides perform a three-step handshake:

    1. SYN – The client sends a synchronize (SYN) message to the server.
    2. SYN-ACK – The server responds with a SYN-ACK (synchronize-acknowledge).
    3. ACK – The client sends back an ACK, and the connection is established.

    This ensures both parties are ready to start data transfer.


    2. Data Transmission

    • Segmentation: TCP breaks application data into manageable segments.
    • Sequence Numbers: Each byte of data is assigned a number. These help with ordering and detecting loss.
    • Acknowledgements (ACKs): The receiver sends ACKs back to confirm receipt of data.

    3. Reliability Mechanisms

    • Retransmission: If an ACK isn’t received in time, TCP resends the data.
    • Checksums: Each segment includes a checksum to verify data integrity.
    • Duplicate Data Handling: Duplicate segments are detected and discarded.

    4. Flow Control (Sliding Window)

    TCP uses a window size to control how much data can be sent before waiting for an ACK. This:

    • Prevents sender from overwhelming the receiver.
    • Allows multiple packets to be in transit (pipeline).

    5. Congestion Control

    To avoid overloading the network:

    • Slow Start: TCP starts with a small congestion window (cwnd) and increases it exponentially.
    • Congestion Avoidance: Once a threshold is reached, it grows linearly.
    • Fast Retransmit & Fast Recovery: Handle loss without waiting for timeout.

    6. Connection Termination: 4-Way Teardown

    TCP closes a connection using a four-step process:

    1. FIN from one side
    2. ACK from the other
    3. FIN from the second side
    4. ACK from the first side

    Each side closes independently.


    πŸ› οΈ TCP Header (Key Fields)

    Field Purpose
    Source/Destination Port Identifies sending and receiving application
    Sequence Number Indicates the position of data
    Acknowledgment Number Confirms received data
    Flags (SYN, ACK, FIN, etc.) Controls connection states
    Window Size Flow control mechanism
    Checksum Error detection

    🌐 Typical Applications

    TCP is used when data accuracy and order are more important than speed:

    • Web browsing (HTTP, HTTPS)
    • Email (SMTP, IMAP, POP3)
    • File transfers (FTP)
    • Remote terminals (SSH, Telnet)