Skip to main content

Computer Networks

Transport Layer (TCP/UDP)

TCP produces the richest numericals in Computer Networks: sequence and ACK numbers, RTO estimates, window-limited throughput and cwnd evolution. Learn the rules once and trace them round by round.

~6 hrs ~1.5–2 marks · Must-score 10 samples · 9 practice · 10 cards

GATE CSE transport layer — ports and sockets, UDP vs TCP headers and services, three-way handshake and teardown, sequence and acknowledgement numbers, RTT and RTO estimation, sliding window and flow control, slow start, AIMD congestion control and token-bucket shaping with numericals, samples, flashcards and drills.

Learning outcomes

  • Compare UDP and TCP services, headers and port usage
  • Trace the three-way handshake, data transfer and connection teardown with sequence and ACK numbers
  • Estimate RTT and RTO and compute window-limited throughput and bandwidth-delay product
  • Trace cwnd and ssthresh through slow start, congestion avoidance, timeout and fast recovery

Ports, UDP and the TCP header

  • The transport layer provides PROCESS-TO-PROCESS delivery. PORT numbers (16 bits, 0 to 65,535) identify processes: well-known ports 0 to 1023, registered 1024 to 49,151, dynamic/ephemeral 49,152 to 65,535. A SOCKET address = IP address + port. Multiplexing and demultiplexing use ports.
  • UDP is CONNECTIONLESS and unreliable: no handshake, no ordering, no retransmission, no flow or congestion control. Its header is only 8 bytes: source port, destination port, length, checksum (checksum is optional in IPv4). Used for DNS, DHCP, SNMP, streaming, VoIP and games.
  • TCP is CONNECTION-ORIENTED, reliable, full duplex and byte-stream oriented with flow control, congestion control, cumulative acknowledgements and retransmission. Header = 20 bytes minimum (up to 60 with options).
  • TCP header fields: source port 16, destination port 16, SEQUENCE NUMBER 32, ACKNOWLEDGEMENT NUMBER 32, header length (HLEN) 4 bits in units of 4 bytes (so 5 to 15 means 20 to 60 bytes), 6 flags (URG, ACK, PSH, RST, SYN, FIN), WINDOW size 16 bits, checksum 16, urgent pointer 16, options.
  • TCP numbers BYTES, not segments. The sequence number is the number of the first data byte of the segment. The ACK number is the number of the NEXT byte expected (cumulative: everything before it has been received).
  • MSS (maximum segment size) is the maximum data in one segment (typically MTU - 40 = 1460 bytes on Ethernet). The 16-bit window field limits the advertised window to 65,535 bytes unless window scaling is used.
  • UDP CHECKSUM covers a PSEUDO-HEADER (source IP, destination IP, a zero byte, protocol = 17, UDP length) plus the UDP header and data, so a datagram delivered to the wrong host is detected even though the IP header is not part of UDP. The UDP length field counts header + data (minimum 8), hence IP total length = UDP length + 20.
  • DEMULTIPLEXING: a UDP socket is identified by (destination IP, destination port), so datagrams from different senders to the same port reach the SAME socket; a TCP socket is identified by the 4-tuple (source IP, source port, destination IP, destination port), so each client gets its own connection socket.
  • TCP FLAGS: SYN opens a connection and synchronises sequence numbers; ACK means the acknowledgement field is valid (set in every segment after the first SYN); FIN closes one direction; RST aborts or rejects; PSH asks the receiver to deliver data now; URG marks urgent data with the urgent pointer.
  • MSS = MTU - IP header - TCP header = 1500 - 20 - 20 = 1460 bytes on Ethernet. The maximum TCP payload that fits in a single IP datagram without fragmentation is the MSS; using the MSS avoids IP fragmentation. TCP data is a byte stream, so one write can be split over many segments and many writes can be merged.
FeatureUDPTCP
Connectionconnectionlessconnection-oriented (handshake)
Reliability and orderingnoneACKs, retransmission, in-order delivery
Header size8 bytes20 to 60 bytes
Flow and congestion controlnoyes (rwnd, cwnd)
Data unitmessage (datagram) boundaries preservedbyte stream, no boundaries
Typical useDNS, DHCP, streaming, VoIPHTTP, SMTP, FTP, SSH

Worked example

Problem. A TCP header has HLEN = 8. What is the header size and the number of option bytes? A segment with sequence number 1000 carries 500 bytes of data; what ACK number does the receiver send?

Solution. Header size = 8 * 4 = 32 bytes, so options = 32 - 20 = 12 bytes. The segment covers bytes 1000 to 1499, so the receiver sends ACK = 1500 (the next byte expected). UDP sizes: a datagram carrying 100 bytes of data has UDP length = 8 + 100 = 108, IP total length = 108 + 20 = 128 and, over Ethernet, a frame of 128 + 18 = 146 bytes. The fraction of useful bytes is 100 / 146 = 68.5%.

The 8-byte UDP header drawn beside the 20 to 60 byte TCP header with a comparison table
UDP header (8 bytes: ports, length, checksum) vs TCP header (20 to 60 bytes: sequence, ACK, HLEN, flags, window, options).

Connection management, RTT and RTO

  • THREE-WAY HANDSHAKE: (1) client sends SYN with seq = x; (2) server replies SYN + ACK with seq = y and ack = x + 1; (3) client sends ACK with seq = x + 1 and ack = y + 1 (this third segment may already carry data). A SYN consumes ONE sequence number even though it has no data.
  • A FIN also consumes one sequence number; a pure ACK consumes none. TERMINATION is a four-way exchange (FIN, ACK, FIN, ACK) because each direction closes independently (half-close). The side that closes last waits in TIME_WAIT for 2 * MSL before releasing the connection, so that old duplicates die out. RST aborts a connection.
  • SEQUENCE-NUMBER WRAP-AROUND: the 32-bit sequence space is 2^32 bytes, so at B bytes per second the numbers wrap in 2^32 / B seconds (about 34.4 s at 1 Gbps, which is why TCP timestamps and PAWS exist).
  • RTT ESTIMATION (Jacobson/Karels): EstimatedRTT = (1 - alpha) * EstimatedRTT + alpha * SampleRTT with alpha = 1/8; DevRTT = (1 - beta) * DevRTT + beta * |SampleRTT - EstimatedRTT| with beta = 1/4; RTO = EstimatedRTT + 4 * DevRTT. KARN’S ALGORITHM ignores samples from retransmitted segments and doubles RTO on each timeout (exponential backoff).
  • FLOW CONTROL: the receiver advertises its free buffer space as RWND in every ACK. The sender may have at most min(rwnd, cwnd) unacknowledged bytes. If rwnd = 0 the sender sends small zero-window probes (persist timer). TCP uses cumulative ACKs, buffers out-of-order segments, and retransmits on timeout or after THREE DUPLICATE ACKs (fast retransmit).
  • Window-limited THROUGHPUT = W / RTT (W bytes per round trip). Full link utilisation needs W >= BANDWIDTH-DELAY PRODUCT = bandwidth * RTT. Utilisation = min(1, W / (B * RTT)).
  • The first data byte after the handshake has sequence number ISN + 1, and a segment of n data bytes advances the sequence number by n. Data can be sent in the third handshake segment, so the first byte of data reaches the server 1.5 RTT after the client starts (SYN, SYN+ACK, then ACK with data).
  • CONNECTION STATES: LISTEN and SYN_RCVD (server), SYN_SENT (client), ESTABLISHED, FIN_WAIT_1 and FIN_WAIT_2, CLOSE_WAIT and LAST_ACK, TIME_WAIT. A SYN flood fills the server with half-open (SYN_RCVD) connections; SYN cookies defend against it by encoding state in the ISN.
  • WINDOW SCALING: to use a window larger than 65,535 bytes the sender and receiver agree a shift count at the handshake; the real window = field * 2^shift. Bits of sequence number needed so the numbers do not wrap within the maximum packet lifetime T on a link of B bytes per second: 2^bits >= B * T.

Formulas to remember

  • EstRTT = 0.875 * EstRTT + 0.125 * Sample
  • DevRTT = 0.75 * DevRTT + 0.25 * |Sample - EstRTT|
  • RTO = EstRTT + 4 * DevRTT
  • Throughput = W / RTT; BDP = B * RTT
  • Wrap time = 2^32 / (bytes per second)

Worked example

Problem. The client ISN is 5000 and the server ISN is 9000. Write the three handshake segments. Then, with EstimatedRTT = 100 ms and a new sample of 140 ms, find the updated estimate. Finally, find the throughput for a 65,535-byte window and 100 ms RTT.

Solution. SYN seq = 5000; SYN+ACK seq = 9000, ack = 5001; ACK seq = 5001, ack = 9001. New EstimatedRTT = 0.875 * 100 + 0.125 * 140 = 87.5 + 17.5 = 105 ms. Throughput = 65,535 * 8 / 0.1 = 5.24 Mbps. More numericals: on a 100 Mbps link with RTT 50 ms the BDP is 10^8 * 0.05 = 5 x 10^6 bits = 625,000 bytes, but a 65,535-byte window gives only 524,280 / 0.05 = 10.49 Mbps, a utilisation of 10.5%. For 1 Gbps and RTT 40 ms the BDP is 10^9 * 0.04 = 5 x 10^6 bytes; 5 x 10^6 / 65,535 = 76.3, so a window scale shift of 7 (factor 128, window up to 8.39 MB) is needed, giving a 23-bit effective window. At 1 Gbps (125 x 10^6 bytes/s) the 32-bit sequence numbers wrap in 2^32 / (125 x 10^6) = 34.4 s.

Client and server timelines for the TCP three-way handshake with sequence and acknowledgement numbers and the four-segment teardown with TIME_WAIT
Handshake (ISN 5000 / 9000): SYN, SYN+ACK, ACK; teardown: FIN, ACK, FIN, ACK, then TIME_WAIT of 2 x MSL.

Congestion control and traffic shaping

  • The sender keeps a CONGESTION WINDOW cwnd (in MSS) next to the receiver window and sends at most min(cwnd, rwnd). A threshold SSTHRESH separates the two growth phases.
  • SLOW START: cwnd starts at 1 MSS and increases by 1 MSS for every ACK, so it DOUBLES every RTT (1, 2, 4, 8, ...) until it reaches ssthresh (or a loss occurs). CONGESTION AVOIDANCE (additive increase): above ssthresh cwnd grows by about 1 MSS per RTT.
  • On a TIMEOUT (severe congestion): ssthresh = cwnd / 2, cwnd = 1 MSS and slow start restarts. On THREE DUPLICATE ACKs with TCP Reno (mild congestion): ssthresh = cwnd / 2, cwnd = ssthresh (plus 3 MSS during fast recovery) and the sender continues in congestion avoidance. TCP Tahoe treats duplicate ACKs like a timeout.
  • AIMD = Additive Increase, Multiplicative Decrease: +1 MSS per RTT, halve on loss. The window forms a SAWTOOTH between W / 2 and W, so the average window is 0.75 W and the average throughput is 0.75 * W / RTT.
  • LEAKY BUCKET smooths bursty traffic into a constant output rate. TOKEN BUCKET (rate r tokens per second, capacity C) allows bursts: with a full bucket and maximum output rate M the burst lasts S = C / (M - r) seconds, and the burst size is M * S.
  • Congestion control is end-to-end in TCP (loss as a signal); network-assisted schemes include ECN and RED (random early detection) in routers. Flow control protects the RECEIVER; congestion control protects the NETWORK.
  • FAST RETRANSMIT / FAST RECOVERY (Reno): on the third duplicate ACK retransmit the missing segment at once without waiting for the timer, set ssthresh = cwnd / 2 and cwnd = ssthresh + 3 MSS, add 1 MSS for every further duplicate ACK, and when the new ACK arrives deflate cwnd to ssthresh and resume congestion avoidance.
  • GROWTH COUNTS: in slow start after n rounds cwnd = 2^(n - 1) MSS and the total segments sent are 2^n - 1; reaching a window of W MSS takes about log2(W) round trips after the first. A common approximation for loss rate p is throughput = 1.22 * MSS / (RTT * sqrt(p)). AIMD converges to a FAIR share among competing flows.
  • Where the numbers are in bytes: slow start adds one MSS per ACK received; congestion avoidance adds MSS * MSS / cwnd per ACK, which totals about one MSS per RTT. The effective window is min(cwnd, rwnd) and ssthresh is never less than 2 MSS.

Formulas to remember

  • Slow start: cwnd doubles every RTT up to ssthresh
  • Congestion avoidance: cwnd += 1 MSS per RTT
  • Timeout: ssthresh = cwnd / 2, cwnd = 1
  • Three dup ACKs (Reno): ssthresh = cwnd / 2, cwnd = ssthresh
  • Token bucket burst time S = C / (M - r)
Roundscwnd, timeout (Tahoe)cwnd, 3 dup ACKs (Reno)
1 to 5 (slow start to ssthresh 16)1, 2, 4, 8, 161, 2, 4, 8, 16
6 to 10 (congestion avoidance)17, 18, 19, 20, 2117, 18, 19, 20, 21
Loss detected at end of round 10ssthresh = 10, cwnd = 1ssthresh = 10, cwnd = 10
11 to 151, 2, 4, 8, 1010, 11, 12, 13, 14

Worked example

Problem. TCP starts with cwnd = 1 MSS and ssthresh = 8 MSS. Give the cwnd for rounds 1 to 8, then a timeout occurs at the end of round 8; give the next cwnd values and the new ssthresh.

Solution. Slow start: rounds 1 to 4 give 1, 2, 4, 8 (reaches ssthresh). Congestion avoidance: rounds 5 to 8 give 9, 10, 11, 12. Timeout at cwnd = 12: ssthresh = 12 / 2 = 6 and cwnd = 1. Next rounds: 1, 2, 4, 6 (slow start capped at ssthresh), then 7, 8, ... in congestion avoidance. With 3 duplicate ACKs (Reno) at the same point, ssthresh = 6 and cwnd = 6 at once, so the next rounds are 6, 7, 8, 9, ... Token bucket: with capacity C = 1 MB, token rate r = 4 MB/s and maximum output M = 20 MB/s, the burst lasts S = C / (M - r) = 1 / 16 = 62.5 ms and sends M * S = 1.25 MB. AIMD with peak window W = 20 MSS averages 0.75 * 20 = 15 MSS per RTT.

Chart of TCP cwnd against transmission rounds showing slow start, congestion avoidance, a timeout drop to 1 and a Reno recovery at 6
cwnd per round with ssthresh 8: 1, 2, 4, 8, then 9 to 12; a timeout sets ssthresh 6 and cwnd 1 (blue), Reno resumes at 6 (green).

Exam tips

  • SYN and FIN each consume one sequence number; ACK numbers always equal the next byte expected.
  • HLEN is in 4-byte units; the TCP header is 20 to 60 bytes, the UDP header is always 8 bytes.
  • cwnd: slow start doubles per RTT up to ssthresh, then +1 MSS per RTT; timeout resets cwnd to 1, three dup ACKs set cwnd to ssthresh.
  • Throughput = window / RTT; for full utilisation window >= bandwidth * RTT.
  • RTO = EstimatedRTT + 4 * DevRTT; a retransmitted segment’s RTT sample is ignored (Karn).
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for Transport Layer (TCP/UDP) with full explanations. Attempt each question before revealing the answer.

Q1 Classic GATE transport

The size of the UDP header is:

  • A) 4 bytes
  • B) 8 bytes
  • C) 16 bytes
  • D) 20 bytes
Show answer & explanation

Answer: B

UDP has four 16-bit fields (source port, destination port, length, checksum) = 8 bytes.

Q2

A client sends SYN with sequence number 2000. The server’s SYN+ACK carries the acknowledgement number:

  • A) 2000
  • B) 2001
  • C) 2002
  • D) 1999
Show answer & explanation

Answer: B

A SYN consumes one sequence number, so the next expected byte is 2001.

Q3

A TCP segment with sequence number 1000 carries 500 bytes of data. The acknowledgement number returned by the receiver (assuming in-order arrival) is:

  • A) 1000
  • B) 1499
  • C) 1500
  • D) 1501
Show answer & explanation

Answer: C

The segment contains bytes 1000 to 1499, so the next byte expected is 1500.

Q4

A TCP header has the HLEN field equal to 8. The size of the header in bytes is:

  • A) 8
  • B) 20
  • C) 32
  • D) 64
Show answer & explanation

Answer: C

HLEN counts 4-byte words: 8 * 4 = 32 bytes.

Q5

A TCP connection starts in slow start with cwnd = 1 MSS and ssthresh = 16 MSS. Assuming no loss, cwnd after 4 completed rounds of transmission is:

  • A) 8
  • B) 16
  • C) 15
  • D) 32
Show answer & explanation

Answer: B

Slow start doubles cwnd after every completed round: 1 -> 2 -> 4 -> 8 -> 16, and 16 equals ssthresh, where congestion avoidance begins.

Q6

A timeout occurs when cwnd = 20 MSS. The new values of ssthresh and cwnd are:

  • A) 10 and 1
  • B) 10 and 10
  • C) 20 and 1
  • D) 5 and 1
Show answer & explanation

Answer: A

On timeout ssthresh = cwnd / 2 = 10 and cwnd restarts at 1 MSS in slow start.

Q7

In TCP Reno, three duplicate ACKs arrive when cwnd = 20 MSS. After fast recovery completes, cwnd is:

  • A) 1
  • B) 10
  • C) 20
  • D) 40
Show answer & explanation

Answer: B

ssthresh = 20 / 2 = 10 and cwnd is set to ssthresh = 10, then congestion avoidance continues.

Q8

A TCP sender uses a 65,535-byte window and the round-trip time is 100 ms. The maximum throughput is about:

  • A) 524 kbps
  • B) 5.24 Mbps
  • C) 52.4 Mbps
  • D) 655 Mbps
Show answer & explanation

Answer: B

65,535 * 8 bits / 0.1 s = 5.24 Mbps.

Q9

TCP sequence numbers are 32 bits long and the link carries 1 Gbps (125 MB/s). The time for the sequence numbers to wrap around is about:

  • A) 3.4 s
  • B) 34.4 s
  • C) 344 s
  • D) 1 hour
Show answer & explanation

Answer: B

2^32 bytes / (125 * 10^6 bytes/s) = 34.4 s.

Q10

After the final ACK of connection termination, the side that actively closed stays in TIME_WAIT for:

  • A) 0.5 MSL
  • B) 1 MSL
  • C) 2 MSL
  • D) 4 MSL
Show answer & explanation

Answer: C

TIME_WAIT lasts 2 * MSL so that delayed segments expire and the last ACK can be retransmitted if lost.

Topic drill · 9 questions

9 practice questions — Transport Layer (TCP/UDP)

Extra page-specific practice beyond the GATE samples. Attempt all 9, then reveal answers to check your reasoning.

P1

A host sends a FIN segment (no data) with sequence number 5000. The acknowledgement number in the reply is:

  • A) 5000
  • B) 5001
  • C) 5002
  • D) 4999
Show answer & explanation

Answer: B) 5001

A FIN consumes one sequence number, so the next byte expected is 5001.

P2

EstimatedRTT = 100 ms and a new SampleRTT = 140 ms with alpha = 1/8. The new EstimatedRTT is:

  • A) 105 ms
  • B) 110 ms
  • C) 120 ms
  • D) 140 ms
Show answer & explanation

Answer: A) 105 ms

0.875 * 100 + 0.125 * 140 = 87.5 + 17.5 = 105 ms.

P3

A 100 Mbps link has an RTT of 50 ms. The minimum window (in bytes) needed to keep the link fully utilised is:

  • A) 62,500
  • B) 625,000
  • C) 5,000,000
  • D) 6,250,000
Show answer & explanation

Answer: B) 625,000

BDP = 100 * 10^6 * 0.05 = 5 * 10^6 bits = 625,000 bytes.

P4

A token bucket has capacity 250 KB, token rate 2 MB/s and maximum output rate 25 MB/s. A full bucket allows a burst of about:

  • A) 5.4 ms
  • B) 10.9 ms
  • C) 12.5 ms
  • D) 125 ms
Show answer & explanation

Answer: B) 10.9 ms

S = C / (M - r) = 0.25 MB / (25 - 2) MB/s = 0.0109 s, about 10.9 ms.

P5

In the AIMD sawtooth of TCP between W/2 and W, the average window as a fraction of W is:

  • A) 1/2
  • B) 2/3
  • C) 3/4
  • D) 1
Show answer & explanation

Answer: C) 3/4

The window grows linearly from W/2 to W, so the average is (W/2 + W) / 2 = 3W/4.

P6

cwnd starts at 1 MSS with ssthresh = 8 MSS and no loss. The cwnd value in round 6 (round 1 has cwnd = 1) is:

  • A) 8
  • B) 9
  • C) 10
  • D) 32
Show answer & explanation

Answer: C) 10

Rounds 1 to 6: 1, 2, 4, 8, 9, 10.

P7

Which statement about UDP is correct?

  • A) It guarantees in-order delivery
  • B) It provides process-to-process delivery using ports with optional error detection
  • C) It uses a three-way handshake
  • D) It retransmits lost datagrams
Show answer & explanation

Answer: B) It provides process-to-process delivery using ports with optional error detection

UDP adds only ports and a checksum on top of IP; it gives no reliability or ordering.

P8

The receiver advertises rwnd = 0. The sender then:

  • A) closes the connection
  • B) stops sending data and periodically sends zero-window probes
  • C) doubles cwnd
  • D) retransmits the last segment forever
Show answer & explanation

Answer: B) stops sending data and periodically sends zero-window probes

The persist timer triggers small probe segments until the receiver reopens its window.

P9

If rwnd = 20 KB and cwnd = 12 KB, the maximum amount of unacknowledged data the sender may have in flight is:

  • A) 8 KB
  • B) 12 KB
  • C) 20 KB
  • D) 32 KB
Show answer & explanation

Answer: B) 12 KB

The sender window is min(rwnd, cwnd) = 12 KB.

Topic FAQs

Why does TCP need a three-way handshake?

Both sides must agree on initial sequence numbers and confirm that the other side can send and receive. Three messages are the minimum for each side to have its SYN acknowledged.

What is the difference between flow control and congestion control?

Flow control (rwnd) stops a fast sender from overrunning the receiver’s buffer. Congestion control (cwnd) stops senders from overloading the network. The sender uses the minimum of the two windows.

What happens to cwnd and ssthresh on a timeout versus on three duplicate ACKs?

Timeout: ssthresh = cwnd / 2 and cwnd = 1 MSS (slow start). Three duplicate ACKs in Reno: ssthresh = cwnd / 2 and cwnd = ssthresh, followed by congestion avoidance (fast recovery).

Why is UDP still used if it is unreliable?

It has low overhead, no handshake delay and no congestion back-off, which suits DNS lookups, real-time audio/video and applications that implement their own reliability.

Quick revision · 10 cards

10 flashcards — Transport Layer (TCP/UDP)

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 UDP header fields and size?
Q2 TCP header size and HLEN?
Q3 Three-way handshake numbers?
Q4 What does the ACK number mean?
Q5 RTO formula?
Q6 Bandwidth-delay product?
Q7 Slow start vs congestion avoidance?
Q8 Timeout vs 3 duplicate ACKs (Reno)?
Q9 Average AIMD throughput?
Q10 Token bucket burst time?