Skip to content
GATEiit

GATE CSE · Computer Networks

TCP: flow control and congestion control; socket API

TCP sequence numbers and connection setup, flow control with the receive window, congestion control (slow start, congestion avoidance, fast retransmit and recovery), retransmission timers, and the socket API calls.

Before you start

This topic comes before: DNS and HTTP

Concept notes

1. TCP segments, sequence numbers and connections

TCP gives a reliable, in-order, connection-oriented byte stream between two processes, identified by (source IP, source port, destination IP, destination port).

  • Sequence numbers count bytes, not segments. A segment's sequence number is the number of its first data byte.
  • The acknowledgement number is the next byte the receiver expects; ACKs are cumulative.
  • SYN and FIN each consume one sequence number even though they carry no data.
  • The header is 20 bytes without options, up to 60 bytes with options (header length is a 4-bit field counting 32-bit words).

Three-way handshake: client sends SYN (seq =x); server replies SYN-ACK (seq =y, ack =x+1); client sends ACK (seq =x+1, ack =y+1). The first data byte from the client then has sequence number x+1.

Closing takes four segments in general (FIN, ACK in each direction); the side that closes first waits in TIME_WAIT for 2× MSL before the connection is gone, so stray old segments die out.

Wrap-around: the 32-bit sequence space holds 232 bytes. At a sending rate of R bytes per second, sequence numbers repeat after 232/R seconds. On fast links this can be shorter than the segment lifetime, which is why the timestamp option (PAWS) exists.

2. Flow control: the receive window

Flow control stops a fast sender from overflowing the receiver's buffer. The receiver advertises a receive window rwnd (free buffer space) in every segment; the sender keeps LastByteSent−LastByteAcked≤min⁡(rwnd, cwnd) where cwnd is the congestion window. So the effective window is W=min⁡(rwnd,cwnd).

  • The window field is 16 bits, so rwnd≤65535 bytes unless the window scale option is used; it multiplies the field by 2S with S≤14.
  • At most one window of data is sent per round trip, so throughput ≤W/RTT.
  • To use a link fully the window must cover the bandwidth-delay product: W≥bandwidth×RTT.
  • If rwnd=0 the sender stops and runs a persist timer, sending small probes until the window opens.
  • Silly window syndrome (tiny segments): Nagle's algorithm on the sender and delayed window updates on the receiver avoid it.

Flow control protects the receiver; congestion control protects the network. Keep the two apart in MSQ statements.

3. Congestion control

The sender keeps a congestion window cwnd and a slow-start threshold ssthresh. GATE questions count cwnd in MSS per transmission round (one RTT).

  • Slow start (cwnd<ssthresh): start at cwnd=1 MSS and add 1 MSS per ACK, so cwnd doubles every RTT.
  • Congestion avoidance (cwnd≥ssthresh): add 1 MSS per RTT (additive increase).
  • Timeout: ssthresh=cwnd/2, cwnd=1 MSS, back to slow start.
  • Three duplicate ACKs: fast retransmit the missing segment. TCP Reno then does fast recovery: ssthresh=cwnd/2 and cwnd=ssthresh (plus 3 MSS while the duplicates are counted), continuing in congestion avoidance. TCP Tahoe treats it like a timeout (cwnd=1).

Halving on loss and adding 1 per RTT is AIMD (additive increase, multiplicative decrease), which gives the sawtooth shape of Reno.

Convention for the threshold: if doubling would jump past ssthresh, most GATE solutions set cwnd=ssthresh for that round and then grow linearly. Read the question; choose numbers carefully when tracing round by round, and write the window at the start of each round.

TCP congestion window over time048121614812131618transmission round (RTT)cwndssthresh 8timeoutslow startcongestion avoidancenew ssthresh 8
Slow start to ssthresh = 8, linear growth to 16, a timeout, then a new ssthresh of 16/2 = 8.

4. Retransmission timer

TCP retransmits a segment if its ACK does not arrive within the retransmission timeout (RTO). The RTO must track the round-trip time, which varies.

Exponentially weighted moving average of measured samples: EstRTT=(1−α) EstRTT+α⋅SampleRTT,α=1/8 DevRTT=(1−β) DevRTT+β ∣SampleRTT−EstRTT∣,β=1/4 RTO=EstRTT+4⋅DevRTT

The older original scheme used RTO=2×EstRTT; use whichever the question states.

  • Karn's algorithm: do not take RTT samples from retransmitted segments (the ACK is ambiguous), and double the RTO after each timeout (exponential backoff).
  • Fast retransmit usually recovers a single loss before the timer fires, because three duplicate ACKs arrive within about one RTT.

When a question gives several samples, apply the update once per sample, in order, carrying the new estimate forward. If it asks for both EstRTT and DevRTT, follow the order it specifies, since different texts update them in different orders.

5. The socket API

Applications use TCP and UDP through sockets. The usual blocking call sequence is:

Server (TCP) Client (TCP)
socket() create endpoint socket()
bind() attach local address and port (optional bind())
listen() mark as passive, set backlog
accept() blocks until a client connects; returns a new connected socket connect() starts the three-way handshake
recv() / send() send() / recv()
close() close() sends FIN

Points GATE tests:

  • listen() does not block and does not accept; accept() blocks.
  • The listening socket keeps listening; each accept() returns a separate socket per client, so a server can serve many clients on the same port.
  • connect() on a TCP socket triggers the handshake; it returns after the SYN-ACK arrives.
  • UDP has no connection: socket(), bind() (server), then sendto() / recvfrom() with the peer address each time. There is no listen() or accept().
  • bind() with a port already in use fails.

Formulas to remember

Effective window
W=min⁡(rwnd, cwnd)
Throughput bound
Throughput≤WRTT
Window to fill the pipe
W≥bandwidth×RTT
Sequence number wrap-around time
Twrap=232 bytesrate in bytes/s
RTT estimateα=1/8 usually
EstRTT=(1−α) EstRTT+α SampleRTT
Deviation estimateβ=1/4 usually
DevRTT=(1−β) DevRTT+β ∣SampleRTT−EstRTT∣
Timeout
RTO=EstRTT+4 DevRTT
On timeout
ssthresh=cwnd/2,cwnd=1 MSS

Common mistakes

  • Forgetting that SYN and FIN consume a sequence number, which shifts every later byte number by one.
  • Confusing flow control (receiver buffer, rwnd) with congestion control (network, cwnd); the sender uses the minimum of the two.
  • Treating three duplicate ACKs like a timeout in TCP Reno; only Tahoe drops cwnd to 1 MSS.
  • Mixing bytes and bits in throughput: window in bytes times 8, divided by RTT in seconds.
  • Saying listen() blocks or that accept() reuses the listening socket.
  • Doubling cwnd past ssthresh during slow start without checking the convention the question uses.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Tracing the congestion window

numerical

TCP starts with cwnd = 1 MSS and ssthresh = 8 MSS. Every transmission round succeeds until a timeout occurs in round 12. Give cwnd for rounds 1 to 17.

  1. Step 1: Slow start: double each round until ssthresh.

    Try it on paper first.

Example 2: Window-limited throughput and window scaling

numerical

A TCP connection has RTT 20 ms and the receiver advertises the maximum unscaled window of 65,535 bytes. (a) What is the maximum throughput? (b) On a 1 Gbps link with RTT 10 ms, what is the smallest window scale shift S that lets the window fill the pipe?

  1. Step 1: (a) Divide one window by one RTT.

    Try it on paper first.

Example 3: Sequence number wrap-around

numerical

How long does it take for TCP sequence numbers to wrap around on a 1 Gbps link sending at full rate? And on a 10 Gbps link?

  1. Step 1: Convert the rate to bytes per second.

    Try it on paper first.

Example 4: Which socket call does what?

conceptual

A TCP server calls socket, bind, listen and accept; a client calls socket and connect. At which call does the three-way handshake happen, and which calls block?

  1. Step 1: Where does the SYN come from?

    Try it on paper first.

Previous year questions

19 official GATE CSE questions on this topic, newest first.

Practise these 19 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.