Computer Science › Section E · Networking and the internet › Topic E.4

Internet protocols: layers, TCP, and UDP

Sending a message across the internet involves software on your device, on the destination and on every router in between. That software is organised into layers, each solving one part of the problem, and at the transport layer an application chooses between two protocols with very different promises: TCP and UDP.

Why layers?

Moving a message between programs on two machines raises separate problems: naming the destination program, splitting the data, finding a route and pushing bits along each link. Internet software tackles them in a stack of layers. Each layer serves the layer above and relies only on the service of the layer below, without caring how it works, so one layer can be replaced, say Wi-Fi for Ethernet, without changing the others. Each layer on the sender is matched by the same layer on the receiver, which undoes its work.

The four layers

The internet's model has four layers. The application layer holds the programs that use the network, such as browsers, mail servers and DNS. The transport layer delivers data between programs: it splits outgoing data into segments, numbers them and reassembles them at the destination. The network layer, also called the internet layer, moves each packet from network to network towards its destination host. The link layer transmits packets across one physical network, using that network's own rules, such as Ethernet's or Wi-Fi's. The whole family of standards is the TCP/IP protocol suite, named after two of its many protocols.

Encapsulation

On the way down the stack, each layer wraps what it receives inside its own header, a block of control information, like putting a letter in an envelope and that envelope in a larger one. The transport header carries port numbers and sequence numbers; the network header carries the source and destination IP addresses; the link layer adds a frame header with local hardware addresses. At the destination each layer strips off its own header and passes the rest up. To forward a packet, routers in between use only the link and network layers: they unwrap the frame, read the IP header to choose the next hop, and rewrap the packet for the next link.

Worked example: one message, three packets

A program sends a 3,000-byte message over Ethernet, where a segment typically carries at most 1,460 bytes of data. 1. The transport layer splits the data: \(1{,}460 + 1{,}460 + 80 = 3{,}000\) bytes, so 3 segments. 2. It adds a TCP header, 20 bytes without options, to each, with sequence numbers so the receiver can restore the order. 3. The network layer adds a 20-byte IP header to each, giving packets of 1,500, 1,500 and 120 bytes. 4. The link layer frames each packet and sends it; the packets may take different routes. Check: the headers add \(3 \times 40 = 120\) bytes, so 3,120 bytes leave, of which headers are about 3.8%.

Ports: finding the right program

An IP address identifies a host, but a host runs many networked programs at once. The transport layer tells them apart with port numbers, 16-bit values from 0 to 65,535. A server listens on an agreed port: 80 for HTTP, 443 for HTTPS, 25 for SMTP mail transfer and 53 for DNS. Ports 0 to 1023 are reserved for such well-known services. The client's side of a conversation gets a temporary port chosen by its operating system. An IP address and a port together identify one program on one machine, much as a building address and a flat number identify one home.

TCP: reliable and connection-oriented

The Transmission Control Protocol (TCP) promises that data arrives complete and in order. Before sending data it sets up a connection with a three-way handshake: the client sends SYN, the server answers SYN-ACK, and the client replies ACK. TCP numbers every byte it sends; the receiver acknowledges what has arrived, and anything not acknowledged in time is sent again. TCP also provides flow control, slowing down so it does not overwhelm the receiver, and congestion control, slowing down when the network between them is overloaded. The cost is extra messages and delay. Web pages, email and file transfers use TCP because every byte matters.

UDP: lightweight and connectionless

The User Datagram Protocol (UDP) simply sends each message, called a datagram, to the given address and port, with no handshake, no acknowledgements and no retransmission. It is connectionless: the sender does not even know whether the destination is running. UDP is called unreliable, but that is not the same as bad. With less overhead and no waiting, it suits short exchanges such as DNS lookups, where the program can simply ask again, and live voice, video and games, where a late packet is useless anyway. An application using UDP must handle any losses itself.

IP: forwarding, routing and time to live

The Internet Protocol (IP) runs the network layer. Forwarding is relaying each packet towards its destination using the router's forwarding table. Routing is keeping those tables up to date as links fail or congest; routers exchange route information with routing protocols, and the Border Gateway Protocol (BGP) does this between the large networks of different organisations. IP is best effort: it may drop, duplicate or reorder packets, leaving reliability to TCP. Each packet carries a time to live (TTL), a hop count that every router reduces by one. A packet whose TTL reaches 0 is discarded, so a packet caught in a routing loop cannot circle forever.

Worked example: TTL and traceroute

A host sends a packet with a TTL of 64, a common starting value, to a server 12 routers away. 1. Each router subtracts 1 before forwarding, so the packet arrives with \(64 - 12 = 52\). 2. The traceroute tool exploits the rule: it sends a probe with TTL 1, which the first router reduces to 0, discards, and answers with a time exceeded message that reveals the router's address. 3. A probe with TTL 2 reveals the second router, and so on. 4. When a probe finally reaches the server, the whole path is known. Check: 12 routers need 13 rounds of probes, the last one reaching the server itself.

The OSI model and the move to IPv6

The International Organization for Standardization published the Open Systems Interconnection (OSI) reference model, with seven layers: physical, data link, network, transport, session, presentation and application. It is widely used as a teaching vocabulary, but the four-layer TCP/IP model was already running the internet when OSI was published, and it remained the real standard. At the network layer, IPv4 and IPv6 run side by side: IPv6 adoption has grown steadily, but IPv4 remains in wide use, so devices commonly support both.

Common mistakes to avoid

First, saying TCP makes IP reliable by changing IP; IP stays best effort, and TCP recovers losses at the end hosts. Second, calling UDP useless because it is unreliable. Third, thinking routers run TCP for traffic passing through; they use only the network and link layers. Fourth, confusing port numbers with IP addresses: the address picks the host, the port picks the program. Fifth, assuming TTL counts seconds; in practice it counts hops. Sixth, mixing up flow control, which protects the receiver, with congestion control, which protects the network.

Key takeaways

Internet software has four layers: application, transport, network and link, and each wraps data in its own header. Routers forward packets using only the network and link layers. The transport layer splits data into segments and delivers it to the right program by port number, such as 443 for HTTPS. TCP is connection-oriented and reliable, with a SYN, SYN-ACK, ACK handshake, acknowledgements, retransmission, flow control and congestion control. UDP is connectionless, fast and unreliable, suiting DNS and live media. IP forwards and routes packets on a best-effort basis, and TTL stops packets circling forever. OSI is a seven-layer reference model.

Flashcards

Flashcard

Four internet layers

Application, transport, network (internet), link

Top to bottom.

Test your understanding

Question 1 of 5

Which layers of internet software does a router use to forward a packet passing through it?

Question 2 of 5

Why does a live video call usually send its audio and video over UDP rather than TCP?

Question 3 of 5

A packet leaves its source with a TTL of 64 and is forwarded by 15 routers on the way to its destination. What is its TTL on arrival?

Question 4 of 5

A server runs a web service and a mail service on the same IP address. How does its transport layer deliver each arriving message to the right program?

Question 5 of 5

A program sends a 4,000-byte message using TCP, and each segment can carry at most 1,460 bytes of data. How many segments are needed?

← Previous topicNext topic →