Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

TCP vs UDP: 3-Way Handshake, Congestion Control & Transport Mechanisms

Lesson 14 of 15 in the free Computer Networks notes on Siksha Sarovar, written by Rohit Jangra.

TCP vs UDP: 3-Way Handshake, Congestion Control & Transport Mechanisms

---

1. TCP (Transmission Control Protocol)

TCP provides reliable, ordered, error-checked delivery of a stream of bytes between applications.

TCP Header Fields

FieldSizePurpose
Source Port16 bitsSending process port number
Destination Port16 bitsReceiving process port number
Sequence Number32 bitsByte offset of first byte in this segment
Acknowledgement Number32 bitsNext byte expected from the sender
Header Length4 bitsSize of TCP header in 32-bit words
Flags6 bitsSYN, ACK, FIN, RST, PSH, URG
Window Size16 bitsReceiver's available buffer space (flow control)
Checksum16 bitsError detection for header + data
Urgent Pointer16 bitsPoints to urgent data (if URG flag set)
OptionsVariableMSS, SACK, timestamp, window scaling

TCP Flags

FlagMeaningUsage
SYNSynchronise sequence numbersConnection establishment
ACKAcknowledgement field is validAfter initial SYN
FINSender finished sendingConnection teardown
RSTReset connectionError, abort connection
PSHPush data to application immediatelyReal-time data
URGUrgent data presentOut-of-band data

---

2. TCP 3-Way Handshake (Connection Establishment)

The 3-way handshake synchronises sequence numbers between client and server before data transfer begins.

Handshake Steps

StepDirectionMessagePurpose
1Client → ServerSYN (seq=x)Client proposes initial sequence number x
2Server → ClientSYN-ACK (seq=y, ack=x+1)Server proposes its sequence y; acknowledges x
3Client → ServerACK (ack=y+1)Client acknowledges server's sequence y

After step 3, the connection is established and data transfer begins.

Security note: SYN flood attacks exploit the half-open state during the handshake — server allocates resources after SYN but before the final ACK. SYN cookies prevent this by not allocating state until the handshake completes.

---

3. TCP 4-Way Teardown (Connection Termination)

StepDirectionMessagePurpose
1Client → ServerFINClient done sending
2Server → ClientACKServer acknowledges
3Server → ClientFINServer done sending
4Client → ServerACKClient acknowledges

After step 4, the client enters TIME_WAIT state for 2 × MSL (Maximum Segment Lifetime, typically 2 minutes) to ensure the final ACK was received.

---

4. TCP Flow Control

Flow control prevents the sender from overwhelming the receiver's buffer.

TCP uses a sliding window mechanism:

  • Receiver advertises its available buffer space as Window Size in each ACK
  • Sender limits unacknowledged bytes to the receiver's window size
  • If window = 0, sender stops and waits for a window update

Example:

  • Receiver buffer = 65535 bytes; currently 30000 bytes in use
  • Receiver advertises Window = 35535
  • Sender can only send 35535 more bytes before waiting for ACKs

---

5. TCP Congestion Control

Congestion control prevents the sender from overwhelming the network (distinct from flow control which protects the receiver).

TCP Congestion Control Phases

PhaseMechanismBehaviour
Slow StartExponential increaseCWND starts at 1 MSS; doubles each RTT
Congestion AvoidanceAdditive IncreaseCWND increases by 1 MSS per RTT (linear)
Fast Retransmit3 duplicate ACKsRetransmit lost segment immediately
Fast RecoverySkip Slow StartHalve CWND, enter Congestion Avoidance

Threshold (ssthresh):

  • Initially high (e.g., 65535 bytes)
  • On timeout: ssthresh = CWND/2; restart Slow Start from 1 MSS
  • On 3 duplicate ACKs: ssthresh = CWND/2; set CWND = ssthresh; enter Fast Recovery

AIMD (Additive Increase Multiplicative Decrease): The core TCP congestion algorithm — gradually increases sending rate; halves it on congestion signal.

---

6. UDP (User Datagram Protocol)

UDP provides a minimal, connectionless transport service — fast but unreliable.

UDP Header (Only 8 bytes!)

FieldSizeDescription
Source Port16 bitsSending port
Destination Port16 bitsReceiving port
Length16 bitsTotal UDP datagram length
Checksum16 bitsError detection (optional in IPv4, mandatory in IPv6)

When to Use UDP

Applications that prefer speed over reliability:

  • DNS: Single query/response — retransmit manually if no reply
  • VoIP/Video streaming: A late/retransmitted packet is useless; just play silence or skip frame
  • Online gaming: Position updates sent at 60fps — old data is irrelevant by the time it could be retransmitted
  • DHCP: Broadcast-based — can't use connection-oriented protocol before having an IP address
  • TFTP (Trivial FTP): Implements its own simple reliability in the application layer

---

7. TCP vs UDP Summary Table

FeatureTCPUDP
ConnectionConnection-oriented (3-way handshake)Connectionless
ReliabilityGuaranteed (retransmission, ACKs)Best-effort (no retransmission)
OrderingYes (sequence numbers)No
Flow controlYes (window size)No
Congestion controlYes (CWND, AIMD)No
Header size20–60 bytes8 bytes
Delivery speedSlower (overhead + ACK wait)Faster
Use casesHTTP, SMTP, FTP, SSHDNS, VoIP, streaming, gaming
Exam Tip: TCP 3-way handshake: SYN → SYN-ACK → ACK. TCP 4-way teardown: FIN → ACK → FIN → ACK. Flow control = protect receiver (window size). Congestion control = protect network (CWND). These are very high-frequency exam topics.

---

Study Deep: Why TCP Congestion Control Is Critical

  • Internet stability: TCP's AIMD congestion control is why the internet doesn't collapse under load. Without it, all senders would transmit at maximum rate, causing router queues to fill and drop packets, which would cause even more retransmissions — a catastrophic feedback loop called congestion collapse.
  • QUIC (HTTP/3): Google developed QUIC to overcome TCP's limitations — QUIC provides reliability, ordering, and congestion control over UDP, and eliminates TCP's head-of-line blocking. QUIC's connection establishment is faster (0-RTT for resumed connections).
  • TCP BBR: Google's Bottleneck Bandwidth and Round-trip propagation time (BBR) algorithm replaces AIMD with a model-based approach that achieves higher throughput on high-bandwidth, high-latency links.