Sub topic 1
Where the transport and application layers sit
Prerequisites
This chapter assumes you already know the following:
- The four-layer model (application, transport, network, link) and the idea of encapsulation.
- Bandwidth, latency, propagation delay, transmission delay, and round-trip time (RTT).
- Powers of two and basic binary and decimal arithmetic.
- Elementary Python or C, enough to read a short script.
Here you will place the transport and application layers in the stack and see what each one is responsible for. You will also learn the vocabulary the rest of the tutorial uses. Later chapters build on it: sockets, UDP, TCP, DNS, and HTTP.
The four layers and who owns what
The Internet stack splits the work of sending data between programs on different machines into four layers. Each layer talks to the layer above and below it, and it talks to its peer layer on the other host through a protocol.
| Layer | Job | Unit of data | Example protocols | Addressing |
|---|---|---|---|---|
| Application | Defines what messages programs exchange | Message | HTTP, DNS, SMTP | Names such as `example.com` |
| Transport | Delivers data between processes | Segment (TCP) or datagram (UDP) | TCP, UDP | Port number |
| Network | Delivers packets between hosts | Packet or datagram | IP | IP address |
| Link | Delivers frames across one hop | Frame | Ethernet, Wi-Fi | MAC address |
GATE questions often test the unit names, so learn them. TCP produces segments. UDP produces user datagrams. IP produces packets, which are also called datagrams. The link layer produces frames.
The network layer gets a packet to the right host. It does not know which program on that host should receive the data. That job belongs to the transport layer.
What the transport layer adds
IP gives a best-effort service between hosts. Packets can be lost, duplicated, delayed, or reordered, and IP makes no promise about any of this. The transport layer builds on that service and adds the following.
- Process-to-process delivery. A port number identifies the receiving process on a host. A 16-bit port field allows values 0 to 65535.
- Multiplexing and demultiplexing. The sender multiplexes data from many processes into one stream of packets. The receiver demultiplexes by reading the port numbers and handing each segment to the right socket.
- Optional extras. TCP adds reliable, in-order delivery, flow control, and congestion control. UDP adds almost nothing beyond ports and an optional checksum.
The transport protocols run only in the end hosts. Routers look at the IP header and forward the packet. They do not read TCP or UDP headers in normal operation. This is the end-to-end principle: reliability is implemented at the edges, not inside the network.
Where the application layer sits
The application layer defines the messages that two programs exchange. It also defines their syntax (for example, text lines ending in CRLF), their meaning, and the order in which they are sent. An application-layer protocol does not move bytes between hosts. It relies on the transport layer for that.
The application chooses its transport according to its needs.
| Protocol | Transport | Well-known port | Why |
|---|---|---|---|
| HTTP | TCP | 80 | Needs every byte, in order |
| HTTPS | TCP | 443 | Same, with TLS on top |
| SMTP | TCP | 25 | Mail must not lose data |
| DNS | UDP (TCP for large responses and zone transfers) | 53 | Short query and reply, so avoid handshake cost |
| DHCP | UDP | 67 and 68 | Client has no IP address yet |
Ports are split into ranges. Well-known ports run from 0 to 1023. Registered ports run from 1024 to 49151. Dynamic or ephemeral ports run from 49152 to 65535. A client usually gets an ephemeral port from the operating system, and a server listens on a well-known one.
Encapsulation across the two layers
When a browser sends an HTTP request, each layer wraps the data from the layer above with its own header.
The following script computes the size at each layer for a 1000-byte HTTP message. It assumes minimum-size headers: 20 bytes for TCP, 20 for IP, and 18 for an Ethernet frame (14-byte header plus 4-byte trailer).
http_message = 1000 # bytes produced by the application
tcp_header = 20 # minimum TCP header
ip_header = 20 # minimum IPv4 header
ethernet_overhead = 18 # 14-byte header + 4-byte FCS
segment = http_message + tcp_header
packet = segment + ip_header
frame = packet + ethernet_overhead
print("TCP segment:", segment)
print("IP packet: ", packet)
print("Ethernet frame:", frame)
print("Header overhead: %.1f%%" % (100 * (frame - http_message) / frame))The output is 1020, 1040, and 1058 bytes, with about 5.5% overhead. Overhead is a larger fraction of the frame when payloads are small, which matters for throughput questions later in the tutorial.
Connection-oriented versus connectionless
The two transport protocols give the application two different services.
- TCP is connection-oriented. The two ends set up state first (a handshake), then exchange a reliable byte stream. TCP does not preserve message boundaries. Two
sendcalls of 100 bytes each may arrive as onerecvof 200 bytes. - UDP is connectionless. Each datagram is independent. Message boundaries are preserved, so one
sendtoof 100 bytes arrives as one datagram of 100 bytes, or it is lost. There is no handshake, no retransmission, and no ordering guarantee.
Neither protocol guarantees bandwidth or delay. Those guarantees are not offered by the Internet transport layer.
How a segment finds its socket
A receiving host identifies a TCP connection by a four-value tuple: source IP, source port, destination IP, destination port. Two clients that connect to port 80 of the same server have different source IP or source port values, so the server treats them as separate connections. This is why a web server can keep thousands of connections on a single port 80.
UDP demultiplexing is simpler. A socket is identified by destination IP and destination port only. Datagrams from different senders that carry the same destination pair arrive at the same socket.
Common errors
- Confusing the layers' addresses. A MAC address changes at every hop. An IP address stays the same from source to destination, apart from NAT. A port identifies a process.
- Saying "TCP is faster than UDP", or the reverse. UDP avoids handshake and retransmission delay, but speed is not what separates them. The difference is the service they provide.
- Thinking HTTP is reliable by itself. HTTP gets reliability from TCP. HTTP/3 runs over QUIC on UDP, and QUIC supplies its own reliability.
- Assuming DNS uses only UDP. DNS uses TCP for zone transfers and for responses too large for UDP.
- Assuming routers run TCP. Routers do not terminate TCP connections. Only the end hosts do. Middleboxes such as NAT devices and firewalls can read port numbers, but they are not part of the basic model.
Exercises
Exercise
A client opens a connection to a web server at port 80 and receives 3000 bytes of data. Which layer's header carries the port number, and which carries the IP address? Name the protocol data unit produced at each of those two layers when using TCP.
Show answer
The transport layer (TCP) header carries the port numbers, and the PDU is a segment. The network layer (IP) header carries the IP addresses, and the PDU is a packet (datagram).
Exercise
A host sends 500 bytes of application data over UDP. The UDP header is 8 bytes, the IP header is 20 bytes, and the Ethernet overhead is 18 bytes. What is the frame size, and what fraction of it is header and trailer overhead (to one decimal place)?
Show answer
Frame = 500 + 8 + 20 + 18 = 546 bytes. Overhead = 46 bytes. 46 / 546 = 8.4%.
Exercise
Two clients with different IP addresses both send TCP segments to port 80 on the same server. Explain how the server tells the connections apart, and say whether the same would apply to UDP datagrams sent to one UDP socket.
Show answer
TCP identifies a connection by the four-tuple (source IP, source port, destination IP, destination port), so the two connections differ in the source IP and are kept separate. A UDP socket is identified only by destination IP and destination port, so datagrams from both clients are delivered to the same socket.