Chapter 04 · CN
Transport Layer (TCP & UDP)
3 topics · 1 sections · beginner-friendly study notes · Updated
The transport layer provides process-to-process communication.
This chapter compares UDP and TCP, explains ports and sockets, walks through the TCP three-way handshake and connection teardown, and gives an overview of flow control (sliding window) and congestion control (slow start, congestion avoidance).
TCP/UDP behaviour, handshake numerics, window sizes and congestion-window traces are regular GATE questions.
Choosing TCP for web and file transfer, UDP for DNS, streaming and games; tuning timeouts and window sizes; socket programming.
Ports, sockets and UDP
A port is a 16-bit number identifying an application endpoint on a host. A socket is the combination of IP address and port number (and for TCP, a connection is identified by the 4-tuple source IP, source port, destination IP, destination port). UDP is a connectionless, unreliable, message-oriented transport protocol with an 8-byte header.
In short: IP address finds the building; port number finds the apartment. UDP just posts the message with no promises.
Think about it
Applications that tolerate loss or need minimal delay avoid TCP overhead.
Port ranges: well-known 0-1023 (HTTP 80, HTTPS 443, FTP 21, SSH 22, SMTP 25, DNS 53), registered 1024-49151, dynamic/ephemeral 49152-65535.
UDP header: source port, destination port, length, checksum (4 fields × 2 bytes = 8 bytes).
It provides multiplexing and optional error detection but no connection set-up, no ordering, no retransmission and no congestion control, which gives low latency and low overhead.
Used by DNS, DHCP, TFTP, SNMP, VoIP, video streaming and online games, and by QUIC as a base.
UDP supports broadcast and multicast; TCP supports only unicast.
TCP features and segment
TCP is a connection-oriented, reliable, byte-stream protocol offering ordered delivery, retransmission of lost data, flow control and congestion control, over full-duplex connections.
In short: TCP is like a phone call plus registered post: it connects first, numbers every byte, and resends what is lost.
Think about it
Most Internet applications need reliable ordered delivery.
Header fields: source port, destination port, sequence number (32 bits, number of the first byte in the segment), acknowledgement number (next byte expected, cumulative), header length, flags (SYN, ACK, FIN, RST, PSH, URG), window size (receiver advertised window), checksum, urgent pointer, options (MSS, window scale, timestamps).
Reliability uses cumulative ACKs, timeouts based on estimated RTT (RTO = EstimatedRTT + 4 × DevRTT) and fast retransmit after three duplicate ACKs.
MSS = MTU - IP header - TCP header (1500 - 20 - 20 = 1460 bytes on Ethernet).
Because TCP is a byte stream it does not preserve message boundaries, unlike UDP.
Key points
- MSS: Maximum segment size: largest data a segment carries.
- Cumulative ACK: ACK n means all bytes before n have been received.
- RTO: Retransmission timeout.
- Sequence number counts bytes, not segments. SYN and FIN each consume one sequence number.
Read more
TCP vs UDP Comparison
TCP (Transmission Control Protocol) is connection-oriented, reliable, ordered, byte-stream, flow- and congestion-controlled and has a 20-60 byte header. UDP (User Datagram Protocol) is connectionless, unreliable, message-oriented, with no flow or congestion control and an 8-byte header. Both use ports for process-to-process delivery and both carry a checksum.
In short: TCP is registered post with tracking and reordering; UDP is dropping a postcard in the box — fast and cheap, no guarantees.
Think about it
A direct GATE, CCNA and interview comparison; it also guides real protocol choices for streaming, DNS and games.
Connection: TCP needs a 3-way handshake before data and a FIN/ACK teardown; UDP sends immediately.
Reliability: TCP uses sequence numbers, cumulative ACKs, retransmission and checksum; UDP offers only an optional error-detecting checksum (mandatory in IPv6).
Ordering: TCP delivers bytes in order and removes duplicates; UDP datagrams may arrive out of order, duplicated or lost.
Data model: TCP is a byte stream without message boundaries; UDP preserves message boundaries (one send = one datagram).
Control: TCP has sliding-window flow control and congestion control (slow start, AIMD); UDP has none, so applications can saturate the network.
Header: TCP minimum 20 bytes (source port, destination port, seq, ack, header length, flags, window, checksum, urgent pointer, options); UDP 8 bytes (source port, destination port, length, checksum).
Speed/overhead: UDP has lower latency, no connection state and supports broadcast and multicast; TCP supports only unicast and keeps per-connection state.
Typical uses
- 1TCP — HTTP/HTTPS, SMTP, FTP, SSH, database connections
- 2UDP — DNS queries, DHCP, SNMP, TFTP, VoIP, video streaming, online games, QUIC (HTTP/3 builds reliability on top of UDP)
Efficiency example: sending 1 byte of data costs 20 (TCP) + 20 (IP) = 40 bytes header, but 8 + 20 = 28 bytes with UDP.
Choosing: use TCP when correctness matters more than latency; UDP when timeliness matters, loss is tolerable, or the application supplies its own reliability.
Port-level difference: a UDP socket is identified by (destination IP, destination port); a TCP connection by the 4-tuple (source IP, source port, destination IP, destination port), so one TCP server port serves many clients.
Key points
- Connection-oriented: A session is set up and torn down with state kept at both ends (TCP).
- Message-oriented: Boundaries of each send are preserved (UDP datagrams).
- Byte stream: Continuous sequence of bytes with no boundaries (TCP).
- Header sizes: TCP 20–60 bytes, UDP 8 bytes. Protocol numbers in IP: TCP = 6, UDP = 17.
- Header overhead example: 1000 bytes payload
→TCP segment 1020, UDP datagram 1008 (add 20 bytes IP to each). - TCP: reliable, ordered, flow + congestion control, unicast only. UDP: unreliable, unordered, no control, unicast/multicast/broadcast.
Read more
Example
CodeTCP and UDP sockets: // TCP client (reliable stream) int s = socket(AF_INET, SOCK_STREAM, 0); connect(s, (struct sockaddr*)&server, sizeof server); // 3-way handshake send(s, buf, n, 0); recv(s, rbuf, sizeof rbuf, 0); // may return partial data close(s); // FIN // UDP client (datagrams) int u = socket(AF_INET, SOCK_DGRAM, 0); sendto(u, buf, n, 0, (struct sockaddr*)&server, sizeof server); // no connection recvfrom(u, rbuf, sizeof rbuf, 0, NULL, NULL); // one datagram per call
TCP 3-Way Handshake
TCP connection setup: (1) client sends SYN with initial sequence number x; (2) server replies SYN+ACK with its own initial sequence number y and ack = x+1; (3) client sends ACK with ack = y+1. Termination uses FIN and ACK in each direction (four-way close).
In short: Hello? - Hello, I hear you. - Great, I hear you too; then both sides say goodbye separately.
Think about it
Both sides must agree on initial sequence numbers before reliable transfer begins.
SYN consumes one sequence number, so ack = x+1.
Initial sequence numbers are random for security.
The client can send data together with the final ACK.
A passive open means the server listens; an active open means the client connects.
Closing: side A sends FIN, B ACKs (half-close, B may still send), later B sends FIN, A ACKs.
The side that closes first goes to TIME_WAIT for 2 × MSL to ensure the last ACK arrives and to expire old duplicates.
RST aborts a connection immediately.
SYN flood is a DoS attack that exhausts half-open connections; SYN cookies mitigate it.
Total time to start sending the first data byte: 1 RTT for the handshake (the first data can go with the third segment).
Key points
- SYN: Flag requesting synchronization of sequence numbers.
- TIME_WAIT: State of the closing side waiting 2×MSL before releasing the connection.
- Half-open: Server has received SYN and sent SYN+ACK but not got the final ACK.
- Sequence example: Client SYN seq=100; Server SYN+ACK seq=300, ack=101; Client ACK seq=101, ack=301.
Flow control (sliding window)
Flow control prevents a fast sender from overwhelming a slow receiver. TCP uses a sliding window: the receiver advertises its free buffer space (rwnd) and the sender never has more than rwnd unacknowledged bytes in flight.
In short: The receiver tells the sender how much room is left in its inbox.
Think about it
Match sender rate to receiver capacity and keep the pipe full on long links.
Stop-and-wait sends one frame and waits for an ACK.
Efficiency = Tt / (Tt + 2Tp) = 1 / (1 + 2a), where a = Tp / Tt.
A sliding window of size N sends N frames before waiting: efficiency = min(1, N / (1 + 2a)).
For 100 percent utilization N ≥ 1 + 2a.
Go-Back-N: receiver window 1, sender window up to 2^k - 1 with k-bit sequence numbers, retransmits all frames from the lost one.
Selective Repeat: both windows up to 2^(k-1), retransmits only the lost frame.
If the receiver advertises zero window, the sender probes with small segments (persist timer).
Throughput ≤ window size / RTT.
Key points
TCP Congestion Control
Congestion control limits the sending rate to avoid overloading the network. TCP keeps a congestion window (cwnd) and sends min(cwnd, rwnd). It uses slow start, congestion avoidance (AIMD) and reacts to loss.
In short: Start slowly, speed up fast until the first sign of trouble, then creep upward and cut speed sharply on loss.
Think about it
Without congestion control the Internet suffers congestion collapse.
Slow start: cwnd starts at 1 MSS and doubles every RTT (increase by 1 MSS per ACK) until it reaches ssthresh.
Congestion avoidance: cwnd increases by about 1 MSS per RTT (additive increase).
On timeout (strong congestion signal): ssthresh = cwnd/2, cwnd = 1 MSS and slow start restarts (Tahoe and Reno).
On three duplicate ACKs: Tahoe behaves as on timeout; Reno does fast retransmit and fast recovery: ssthresh = cwnd/2, cwnd = ssthresh (+3), continuing in congestion avoidance (multiplicative decrease).
The result is the sawtooth pattern of AIMD.
Flow control protects the receiver; congestion control protects the network.
Router-assisted methods include ECN and active queue management such as RED.
Key points
Read more
Solved examples
Show the segments of a TCP three-way handshake if the client picks initial sequence number 100 and the server picks 300.
Flags, sequence and acknowledgement numbers
Formula Applied:—Approach note: ack = other side’s seq + 1 whenever the segment carried SYN or FIN (or + data length for data).
A stop-and-wait protocol runs on a 1 Mbps link. Frame size is 1000 bits and one-way propagation delay is 20 ms (ignore ACK size). Find the link efficiency and the window needed for 100% utilization.
- B = 1 Mbps
- L = 1000 bits
- Tp = 20 ms
Efficiency and required window
Formula Applied:—Approach note: Sequence-number bits needed for Go-Back-N: 2^k - 1 ≥ 41, so k = 6.
TCP Reno: ssthresh = 8 MSS initially, cwnd starts at 1. Assume no loss for the first transmission rounds, then a timeout occurs when cwnd = 12. Give cwnd per round and the values after the timeout.
- Initial cwnd = 1
- ssthresh = 8
- Timeout at cwnd = 12
cwnd trace, new ssthresh
Formula Applied:—Approach note: For three duplicate ACKs in Reno use cwnd = ssthresh = cwnd/2 instead of restarting from 1.
A TCP connection has a receive window of 64 KB and a round-trip time of 100 ms. What is the maximum throughput?
- Window = 64 KB = 65536 bytes
- RTT = 100 ms
Maximum throughput
Formula Applied:—Approach note: To fill a faster link the window must grow to at least the bandwidth-delay product (window scaling).
Real-world applications
Using Transport Layer: TCP & UDP in real projects
Choosing TCP for web and file transfer, UDP for DNS, streaming and games; tuning timeouts and window sizes; socket programming.
Common mistakes
❌ Myth: UDP is always bad because it is unreliable.
Why this is wrong: Many real-time uses prefer low latency over guaranteed delivery, and applications can add their own reliability.
✅ Verified Truth: UDP is chosen for DNS, VoIP, streaming, gaming and QUIC.
⚠️ Exam Tip: Mention both trade-offs when asked to compare.
❌ Myth: Flow control and congestion control are the same.
Why this is wrong: Flow control protects the receiver’s buffer; congestion control protects the network.
✅ Verified Truth: TCP uses rwnd for flow control and cwnd for congestion control, sending min(rwnd, cwnd).
⚠️ Exam Tip: State who is being protected in each case.
❌ Myth: The TCP sequence number counts segments.
Why this is wrong: It numbers bytes of the stream.
✅ Verified Truth: Seq is the number of the first data byte in the segment; SYN and FIN consume one number each.
⚠️ Exam Tip: Check whether a numerical gives bytes or segments.
Revision notes
The transport layer provides process-to-process communication. This chapter compares UDP and TCP, explains ports and sockets, walks through the TCP three-way handshake and connection teardown, and gives an overview of flow control (sliding window) and congestion control (slow start, congestion avoidance).
High-Yield Quick Revision Points
- Socket = IP + port; ports 0–1023 well-known
- UDP: 8-byte header, no connection, no reliability
- TCP: SYN → SYN+ACK → ACK; close with FIN/ACK each way
- SYN and FIN each consume one sequence number
- Stop-and-wait efficiency 1/(1+2a); window for 100% = 1+2a
- GBN window 2^k - 1; SR window 2^(k-1)
- Slow start doubles per RTT; AIMD; timeout → cwnd = 1, ssthresh = cwnd/2
Practice questions
Which of the following uses UDP by default?
In the TCP handshake the server’s SYN+ACK has ack number 501. The client’s initial sequence number was:
With 3-bit sequence numbers, the maximum sender window size in Go-Back-N is:
TCP congestion window after a timeout is set to:
Stop-and-wait with a = Tp/Tt = 9 has an efficiency of:
The size of the UDP header is: