Computer Science and IT‹›⌂ 0:00 studied

Module 11 · 13 min read · Free chapter

Transport layer: UDP, TCP, flow and congestion control

In short

The transport layer delivers data between processes using ports. UDP has an 8-byte header and no reliability, while TCP numbers bytes and opens with SYN, SYN+ACK and ACK. Throughput is at most window/RTT. Slow start doubles cwnd each RTT up to ssthresh, then growth is linear, and a timeout halves the threshold.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: the building and the flat

A courier brings a parcel to a big apartment building. The building's address gets the parcel to the right gate, but the guard still needs the flat number to hand it to the right family. Hundreds of families live behind one address.

A computer is like that building. The IP address brings a packet to the right machine, but on that machine a browser, a mail app and a music app are all waiting for data. The transport layer uses a number called a port to give each packet to the right program, the way a flat number picks the right family.

There are also two styles of delivery. A registered courier takes a signature, tracks every parcel and sends a lost one again, which is how TCP works. A person handing out pamphlets just throws them at every gate and never checks, which is how UDP works.

Why it matters. Every app you use picks one of these two styles. Choosing the wrong one makes a video call jerky or a bank transfer unsafe, and exams test exactly this choice along with the numbers behind TCP.

Ports, sockets and UDP

A port is a 16-bit number, so it runs from 0 to 65,535. Ports 0 to 1023 are well-known and belong to standard services, such as 80 for web servers, 1024 to 49151 are registered, and 49152 to 65535 are dynamic. A client usually picks a random dynamic port for itself each time it connects.

A socket is an IP address together with a port number, such as 192.168.1.5:50432. A TCP connection is fixed by four values: source IP, source port, destination IP and destination port. That is why one web server on port 443 can talk to thousands of clients at once, since every client brings a different IP address or port.

Programs reach the network through the socket API. A server creates a socket, binds it to a port, listens and accepts connections, while a client creates a socket and connects. Both then send and receive data and finally close the socket.

server: s = socket(); bind(s, port 80); listen(s); c = accept(s); recv(c); send(c); close(c)
client: s = socket(); connect(s, server, 80); send(s); recv(s); close(s)

UDP (User Datagram Protocol) is the simplest transport protocol. It adds just an 8-byte header with four 16-bit fields: source port, destination port, length and checksum. It has no connection setup, no acknowledgements, no ordering and no flow or congestion control.

UDP suits jobs where speed matters more than perfection. A DNS query fits in one packet, and if it is lost the client simply asks again. A live video call would rather skip a lost frame than wait for it, and DHCP must work before the host even has an address.

Worked example 1. What is the largest payload one UDP datagram can carry over IPv4?

  • The IP total length field allows at most 65,535 bytes.
  • Subtracting the 20-byte IP header and the 8-byte UDP header gives 65535−20−8=6550765535 - 20 - 8 = 65507 bytes.

Why it matters. "Which protocol does DNS use?" and "What is the size of the UDP header?" are one-mark favourites. The four-value identity of a TCP connection also explains how NAT and busy servers keep many connections apart.

The TCP segment, handshake and teardown

TCP gives a reliable, ordered byte stream between two processes. It numbers every byte, not every segment, so the sequence number of a segment is the number of its first data byte. The acknowledgement number is the next byte the receiver expects, so it says "I have everything before this".

Field Bits Purpose
Source and destination port 16 each Which processes are talking
Sequence number 32 Number of the first data byte
Acknowledgement number 32 Next byte expected
Header length 4 In 4-byte words, so 20 to 60 bytes
Flags 6 main ones URG, ACK, PSH, RST, SYN, FIN
Receive window 16 Free buffer space at the receiver
Checksum, urgent pointer 16 each Error check; end of urgent data
Options 0 to 320 MSS, window scale, timestamps

The three-way handshake. Before any data flows, the two sides agree on starting numbers. The client sends a SYN with a random initial sequence number xx, and the server replies with SYN+ACK, carrying its own number yy and acknowledgement x+1x + 1. Finally the client sends an ACK with acknowledgement y+1y + 1, and the connection is open.

The SYN carries no data, yet it uses up one sequence number. That is why the server acknowledges x+1x + 1, not xx, and the same is true of FIN. Random starting numbers stop old segments from an earlier connection being mistaken for new ones.

Worked example 2. The client picks 1000 and the server picks 5000. After the handshake the client sends three segments of 500 bytes each.

  • The handshake is SYN (seq 1000), then SYN+ACK (seq 5000, ack 1001), then ACK (ack 5001).
  • The first data byte is number 1001, so the three segments carry sequence numbers 1001, 1501 and 2001.
  • When all three arrive, the server sends acknowledgement 2001+500=25012001 + 500 = 2501.

Closing. Each direction is closed separately, because one side may still have data to send. A side sends FIN, the other replies ACK, and later the other side sends its own FIN, which gets its own ACK, so a normal close uses four segments.

The side that closed first then waits in TIME_WAIT for twice the maximum segment lifetime. This makes sure its last ACK can be resent if lost, and that stray old segments die before the same port pair is used again.

Why it matters. Sequence and acknowledgement numbers in a short exchange are a standard GATE question. Remember that SYN and FIN each count as one byte, and that the ack number points to the next byte, not the last one received.

Flow control, reliability and wrap-around

TCP uses a sliding window, the same idea as on a data link, but counted in bytes. The receiver tells the sender how much free buffer it has, in the receive window field, called rwnd\mathit{rwnd}. The sender keeps at most min⁡(rwnd,cwnd)\min(\mathit{rwnd}, \mathit{cwnd}) bytes in flight without acknowledgement, where cwnd\mathit{cwnd} is the congestion window of the next section.

This is flow control: it protects a slow receiver from a fast sender. If the receiver's buffer fills up, it advertises a window of zero and the sender stops. The sender then sends small probe segments now and then until the window opens again.

TCP uses cumulative acknowledgements, and a lost segment is resent when its retransmission timer runs out. The timeout is set a little above the measured round-trip time, and it is updated as the RTT changes.

A window limits throughput. A sender can push one window of data per round trip, so one connection gets at most

throughput≤WRTT\text{throughput} \le \frac{W}{\mathit{RTT}}

Worked example 3. The largest window the 16-bit field allows is 65,535 bytes, and the RTT is 100 ms.

  • The throughput is at most 65535×80.1≈5.24×106\dfrac{65535 \times 8}{0.1} \approx 5.24 \times 10^6 bits per second, about 5.24 Mbps.
  • To fill a 1 Gbps path with the same RTT, the window must be 109×0.18=1.25×107\dfrac{10^9 \times 0.1}{8} = 1.25 \times 10^7 bytes, about 12.5 MB.
  • So fast long paths need the window scale option, which multiplies the advertised window by a power of 2.

Sequence number wrap-around. Sequence numbers are 32 bits, so after 2322^{32} bytes, about 4.29 billion, they start again from 0. If old segments are still around when the numbers wrap, they could be mistaken for new data.

Worked example 4. How long does it take for the sequence numbers to wrap?

  • At 1 Gbps the sender sends 1.25×1081.25 \times 10^8 bytes per second, so the time is 2321.25×108≈34.4\dfrac{2^{32}}{1.25 \times 10^8} \approx 34.4 seconds.
  • At 10 Mbps the time is 2321.25×106≈3436\dfrac{2^{32}}{1.25 \times 10^6} \approx 3436 seconds, about 57 minutes.
  • At high speeds the wrap time falls below the lifetime of old packets, which is why the timestamps option is used to tell old segments from new.

Why it matters. "Maximum throughput with window W and round-trip time RTT" and "wrap-around time" are two of the most common TCP numericals. Both are one division, as long as you keep bits and bytes straight.

Congestion control: slow start, AIMD and fast retransmit

Flow control looks after the receiver, but routers in the middle can also be swamped. When too many senders push too hard, queues overflow and packets are dropped, which is congestion. TCP has no direct report from routers, so it treats a lost segment as a sign of congestion and slows down.

The sender keeps a congestion window cwnd\mathit{cwnd}, counted here in units of the maximum segment size (MSS). It also keeps a threshold ssthresh\mathit{ssthresh} that separates two phases of growth.

Slow start. A new connection starts with cwnd=1\mathit{cwnd} = 1 MSS. Each arriving ACK adds 1 MSS, so the window doubles every RTT: 1, 2, 4, 8 and so on. The name is misleading, because the growth is exponential, and it is slow only at the very start.

Congestion avoidance. Once cwnd\mathit{cwnd} reaches ssthresh\mathit{ssthresh}, growth becomes linear, about 1 MSS per RTT. This is the additive increase part of the rule.

Reacting to loss. TCP reacts to the two kinds of loss signal differently.

  • On a timeout, TCP sets ssthresh=cwnd2\mathit{ssthresh} = \dfrac{\mathit{cwnd}}{2}, drops cwnd\mathit{cwnd} to 1 MSS and starts slow start again, which is the older Tahoe behaviour.
  • On three duplicate ACKs, TCP resends the missing segment at once without waiting for the timer, which is called fast retransmit.
  • In TCP Reno it then sets ssthresh=cwnd2\mathit{ssthresh} = \dfrac{\mathit{cwnd}}{2} and continues from about that window in congestion avoidance, which is called fast recovery.

Halving the window on loss is the multiplicative decrease part. Together the two parts are called AIMD (additive increase, multiplicative decrease), and they give TCP's typical saw-tooth graph. AIMD also shares a bottleneck link fairly between connections over time.

Worked example 5. A connection starts with cwnd=1\mathit{cwnd} = 1 MSS and ssthresh=16\mathit{ssthresh} = 16 MSS, and a timeout happens when cwnd\mathit{cwnd} has reached 20.

RTT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cwnd (MSS) 1 2 4 8 16 17 18 19 20 1 2 4 8 10 11
  • From RTT 1 to 5 the window doubles in slow start and reaches the threshold 16.
  • From RTT 6 to 9 it grows by one each RTT, until the timeout at 20.
  • The new threshold is 202=10\dfrac{20}{2} = 10, so slow start restarts at 1 and is capped at 10 in RTT 14, after which linear growth resumes.

Note the convention in RTT 14: doubling 8 would give 16, but slow start stops at the threshold, so the window is set to 10. GATE questions usually state this convention, so read the question carefully.

Why it matters. Window-growth tables like this one appear in GATE almost every year. If you write the RTT row and the cwnd row side by side and apply the three rules, these questions become routine.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about the transport layer in four ways:

  1. Compare TCP and UDP, with the applications that suit each.
  2. Explain the three-way handshake and connection release with a labelled diagram.
  3. Distinguish flow control from congestion control and describe how TCP does each.
  4. Trace the congestion window over several round trips for a given loss pattern.

Practice questions (our own, not previous-year questions).

  1. Compare TCP and UDP under the headings connection, reliability, ordering, header size and typical uses. (10 marks)
  2. Explain the three-way handshake with sequence numbers. Why are initial sequence numbers chosen at random? (10 marks)
  3. Describe slow start, congestion avoidance and fast retransmit in TCP. Trace the window for 12 round trips starting from 1 MSS with a threshold of 8 MSS and a timeout at 12 MSS. (15 marks)

Answer plan for question 3.

  • Begin by saying that TCP limits its sending rate by a congestion window because the network gives no direct report of congestion.
  • Explain that slow start doubles the window every round trip until it reaches the threshold.
  • Explain that congestion avoidance then adds one segment per round trip, which is the additive increase.
  • State that a timeout halves the threshold and resets the window to one segment, while three duplicate ACKs trigger fast retransmit and, in Reno, halve the window.
  • Write the trace as 1, 2, 4, 8, 9, 10, 11, 12, then the timeout makes the threshold 6, so the next values are 1, 2, 4 and 6.
  • Conclude that this AIMD behaviour gives the saw-tooth shape and shares a bottleneck link fairly.

Quick revision

  • Ports are 16 bits, and ports 0 to 1023 are well-known.
  • A TCP connection is identified by source IP, source port, destination IP and destination port.
  • UDP has an 8-byte header and gives no reliability, ordering or congestion control.
  • The TCP header is 20 to 60 bytes, and TCP numbers bytes, not segments.
  • The handshake is SYN, SYN+ACK and ACK, and SYN and FIN each use one sequence number.
  • A normal close uses four segments, and the first closer waits in TIME_WAIT.
  • The sender keeps at most min⁡(rwnd,cwnd)\min(\mathit{rwnd}, \mathit{cwnd}) bytes in flight.
  • One connection's throughput is at most WRTT\dfrac{W}{\mathit{RTT}}.
  • Sequence numbers wrap after 2322^{32} bytes, which takes about 34 seconds at 1 Gbps.
  • Slow start doubles cwnd each RTT up to ssthresh, and congestion avoidance adds 1 MSS per RTT.
  • A timeout sets ssthresh to half of cwnd and cwnd to 1, while three duplicate ACKs trigger fast retransmit.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. A TCP connection is uniquely identified by:

  2. Well-known port numbers lie in the range:

  3. The size of the UDP header is:

  4. The largest UDP payload in one IPv4 datagram is:

  5. A TCP segment carries bytes 1001 to 1500. The receiver, having everything before it, sends acknowledgement number:

  6. A client sends SYN with sequence number 1000 and the server replies SYN+ACK with sequence number 5000. The client's final ACK carries acknowledgement number:

  7. After the handshake (client ISN 1000), the client sends three 500-byte segments. Their sequence numbers are:

  8. A normal TCP connection release uses how many segments?

  9. With a 65,535-byte window and an RTT of 100 ms, the maximum TCP throughput is about:

  10. TCP flow control is driven by:

  11. At 1 Gbps, how long do 32-bit TCP sequence numbers take to wrap around?

  12. In slow start, starting from cwnd=1\mathit{cwnd} = 1 MSS with ssthresh=16\mathit{ssthresh} = 16 MSS and no loss, cwnd\mathit{cwnd} after the 6th RTT is:

  13. A timeout occurs when cwnd=20\mathit{cwnd} = 20 MSS. The new ssthresh\mathit{ssthresh} and cwnd\mathit{cwnd} are:

  14. Retransmitting a segment on receiving three duplicate ACKs, without waiting for the timer, is called:

  15. Which topic does the GATE 2026 CS syllabus list under the transport layer?

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What is the size of the UDP header?

It is 8 bytes, with source port, destination port, length and checksum fields of 16 bits each.

Why does the server acknowledge x + 1 in the handshake?

The SYN uses up one sequence number, so the next byte expected is x + 1.

What happens to cwnd after a timeout?

ssthresh becomes half of the current cwnd, and cwnd drops to 1 MSS before slow start begins again.

How long do TCP sequence numbers take to wrap at 1 Gbps?

About 34 seconds, because 2³² bytes divided by 1.25 × 10⁸ bytes per second is about 34.4 s.

More in this module: Computer networks

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Computer Networks and Internet Protocol (IIT Kharagpur, 106105183): transport layer, TCP flow and congestion control