Module 11 · 13 min read · Free chapter
Transport layer: UDP, TCP, flow and congestion control
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.
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 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 , and the server replies with SYN+ACK, carrying its own number and acknowledgement . Finally the client sends an ACK with acknowledgement , and the connection is open.
The SYN carries no data, yet it uses up one sequence number. That is why the server acknowledges , not , 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 .
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 . The sender keeps at most bytes in flight without acknowledgement, where 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
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 bits per second, about 5.24 Mbps.
- To fill a 1 Gbps path with the same RTT, the window must be 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 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 bytes per second, so the time is seconds.
- At 10 Mbps the time is 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 , counted here in units of the maximum segment size (MSS). It also keeps a threshold that separates two phases of growth.
Slow start. A new connection starts with 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 reaches , 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 , drops 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 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 MSS and MSS, and a timeout happens when 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 , 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:
- Compare TCP and UDP, with the applications that suit each.
- Explain the three-way handshake and connection release with a labelled diagram.
- Distinguish flow control from congestion control and describe how TCP does each.
- Trace the congestion window over several round trips for a given loss pattern.
Practice questions (our own, not previous-year questions).
- Compare TCP and UDP under the headings connection, reliability, ordering, header size and typical uses. (10 marks)
- Explain the three-way handshake with sequence numbers. Why are initial sequence numbers chosen at random? (10 marks)
- 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 bytes in flight.
- One connection's throughput is at most .
- Sequence numbers wrap after 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.