A Level Computer Science · Chapter 10 · Component 01: Computer systems
A network lets computers share data by splitting it into packets, sending them by agreed rules called protocols, and finding each destination by its address.
This chapter teaches the hardware and layouts of networks, how packet switching sends data across them, and how the layers of the TCP/IP stack share the work. It then teaches IP addresses and subnets, how the Domain Name System finds a website, and how client–server and peer-to-peer networks compare.
In this chapter · jump to a section
Start here
Your route through this chapter
By the end of this chapter you will be able to:
- describe LANs and WANs, star and mesh topologies, and the job of each piece of network hardware
- explain packet switching and circuit switching, and calculate packets, headers and overheads
- describe the four layers of the TCP/IP stack and explain how encapsulation works
- convert IP addresses to binary, apply a subnet mask and count the usable hosts in a subnet
- explain how the Domain Name System turns a domain name into an IP address
- compare client–server and peer-to-peer networks, and calculate the time to share a file each way
Time: three sessions, about 4 hours, plus the programming task.
You need first: binary from Number systems and binary arithmetic, and Dijkstra's algorithm from Graph traversal and shortest paths.
The big picture
The big picture
A network is two or more devices connected so they can share data and resources. A school network lets every pupil reach the same printers, files and internet connection. The internet is a network of networks: millions of separate networks, run by different organisations, joined together so that any device on one can reach any device on another.
No single organisation runs the internet. Each network sets its own rules inside, but all of them agree to follow the same public protocols at their edges. That agreement lets a phone in one country load a page from a server on another continent. The data passes through dozens of networks run by companies that have never dealt with each other directly.
Sending data across a network raises three questions. How is a large file carried over links that other people are using at the same time? How does every device agree on how the data is laid out, so it can be understood? And how is each destination found among billions of devices?
The answers come together in the internet's design. Data is split into small packets, which travel independently and share the links with everyone else's packets. Protocols, sets of agreed rules, say exactly how each packet is built and handled. They are arranged in layers, so that each layer does one job and relies on the layer beneath. And every device has an IP address, a number that routers use to forward packets towards it.
People do not remember numbers, so the Domain Name System translates names such as a school's web address into IP addresses. Finally, a network may be organised around central servers or as equal peers. That choice decides how well it copes when many people want the same data at once.
Section 1
Networks and their hardware
LANs and WANs
A local area network, or LAN, covers a small area, such as a home, a school or an office building. Its cables and wireless access points are usually owned by the organisation that uses it. A wide area network, or WAN, covers a large geographical area, such as a country, and usually uses links rented from telecommunications companies. The internet is the largest WAN of all. Between the two sit networks such as a university campus, which joins many buildings with its own fibre but still counts as one organisation's network.
LANs are fast and cheap to run, because their links are short and owned outright. WANs connect sites that are far apart, such as a company's offices in different cities, but their links cost money every month and are usually slower than a LAN's.
The speed of a link is its bandwidth, the number of bits it can carry each second, measured in megabits or gigabits per second. Latency is the delay before data starts to arrive, caused by distance and by the time each device takes to handle a packet. A link can have high bandwidth and still feel slow if its latency is high, as anyone who has used a satellite connection knows.
Topologies
A topology is the layout of the connections in a network. In a star topology, every device connects to a central switch. If one cable fails, only that device loses its connection, and new devices are easy to add. But if the switch fails, the whole network goes down, and every device needs its own cable back to the centre.
In a mesh topology, devices are connected to several others, so there are many routes between any two of them. If one link fails, data simply takes another route, which makes mesh networks very reliable. In a full mesh every device connects to every other; in a partial mesh only some do. The internet's routers form a partial mesh, and many home wireless systems use a mesh of access points.
Real networks often mix topologies. A school might wire each classroom as a star around its own switch, then connect the switches to a central core in a star of stars. The links between its main buildings might form a small mesh, so that a single cut cable does not split the site in two.
Network hardware
Each device needs a network interface card, or NIC, to connect to the network. The NIC has a unique MAC address built in, which identifies it on the local network. A MAC address is 48 bits long, usually written as six pairs of hexadecimal digits, and is set when the card is made. A switch connects devices within a LAN and sends each frame only to the device whose MAC address it is addressed to. A wireless access point lets devices join the network by radio.
A router connects different networks together and forwards packets between them, using IP addresses to choose where each packet goes next. A home router joins the home LAN to the internet service provider's network. Cables carry the signals: copper twisted-pair cables are cheap and common in buildings, while fibre-optic cables carry light over long distances at very high speeds.
Wireless networks trade speed and security for convenience. Radio signals spread through walls and can be picked up by anyone nearby, so wireless networks encrypt their traffic. They also share the same radio channel between every device in range, so a crowded classroom of laptops gets less bandwidth each than the same laptops on cables.
Common mistake: thinking a switch and a router do the same job. A switch connects devices within one network, using MAC addresses. A router connects separate networks, such as a home network and the internet, using IP addresses to forward packets between them.
Worked example 1: cabling a star
An office of 24 computers and 2 printers is wired as a star. How many cables are needed, and what happens if one fails?
| Working | Reason |
|---|---|
| Devices = 24 + 2 = 26 | Every computer and printer needs a link |
| Cables = 26, one from each device to the switch | A star has one link per device |
| If one cable fails, only that device is cut off | Every other device has its own link |
| If the switch fails, all 26 are cut off | The switch is a single point of failure |
Worked example 2: a full mesh
Five offices are to be joined in a full mesh, with a direct link between every pair. How many links are needed?
| Working | Reason |
|---|---|
| Each office links to the other 4 | A full mesh joins every pair |
| 5 × 4 = 20 counts each link twice | Each link has two ends |
| Links = 20 ÷ 2 = 10 | Halve to count each link once |
| Adding a sixth office needs 5 more links | Mesh links grow quickly with size |
Check yourself. State a benefit and a drawback of a mesh topology compared with a star.
Show the answer
A mesh is more reliable, because data can take another route if a link fails. But it needs far more links, so it costs more to build, and it is harder to manage.
Section 2
Packet switching
Packets
Data sent across the internet is split into packets. Each packet carries a header and a payload. The payload is a piece of the data. The header holds the source and destination IP addresses and a sequence number saying where the packet fits in the message. It also holds the number of hops it may still make, and a checksum for detecting errors.
In packet switching, each packet is sent independently. Routers forward each one towards its destination by the best route available at that moment. So packets from the same message may travel different routes and arrive in a different order. The receiving computer uses the sequence numbers to put them back in order, and asks again for any that are missing or damaged.
Circuit switching
The older telephone network used circuit switching. A dedicated path between the two ends is set up before any data is sent, and it is kept for the whole call. All the data follows the same path and arrives in order, at a steady rate. But the path is reserved even when nothing is being sent, so capacity is wasted, and setting up the circuit takes time.
Packet switching uses links far more efficiently, because many conversations share every link and a link is only busy while a packet is crossing it. It also copes with failures: if a router breaks, packets are sent another way. The cost is that delivery times vary, and packets must be numbered and reassembled.
Each packet also carries a hop limit, sometimes called its time to live. Every router that forwards the packet reduces the limit by one, and a router that receives a packet with a limit of zero throws it away. This stops a packet circling the network forever if routers are misconfigured into a loop.
Packets can be lost on the way, for example when a router is too busy and has to drop some. With TCP, the receiver sends back acknowledgements saying which packets have arrived. If the sender hears nothing about a packet within a set time, it sends it again. This is how a file always arrives complete, even across links that lose some of their traffic.
Routers decide where to send each packet by looking up its destination in a routing table. The table lists networks and the next router on the way to each. Routers swap information with their neighbours, so that when a link fails or a new one opens, every routing table is updated and packets flow around the change.
Figure 10.1 splits a message into packets. Move the message size and watch the packets and the bytes sent. Then switch on the wrong model.
Splitting a message into packets
- Packets
- 4
- Bytes sent
- 5,160
- Header overhead
- 3.1%
Common mistake: thinking a packet carries only the data. Every packet also carries a header, with addresses, a sequence number and error-checking information. Those header bytes are sent too, so more bytes cross the network than the message itself contains.
Worked example 3: packets for a file
A 5,000-byte message is sent in packets carrying up to 1,460 bytes of data, each with a 40-byte header. How many packets are needed, and how many bytes are sent?
| Working | Reason |
|---|---|
| 5000 ÷ 1460 = 3.42, so 4 packets | The last packet is only partly full |
| Headers: 4 × 40 = 160 bytes | Every packet carries a header |
| Bytes sent = 5000 + 160 = 5,160 | The data plus every header |
| Overhead = 160 ÷ 5160 = 3.1% | The share of the bytes spent on headers |
Worked example 4: a bigger message
The same packets carry a 20,000-byte message. Find the packets, the bytes sent and the overhead.
| Working | Reason |
|---|---|
| 20000 ÷ 1460 = 13.7, so 14 packets | Round up to whole packets |
| Headers: 14 × 40 = 560 bytes | One header per packet |
| Bytes sent = 20000 + 560 = 20,560 | The data plus every header |
| Overhead = 560 ÷ 20560 = 2.7% | Fuller packets waste less on headers |
Derivation: the overhead of packet headers
A message of M bytes is sent in packets that carry up to P bytes of data, each with an H-byte header. The number of packets is the message size divided by the payload, rounded up.
The overhead is the share of the bytes sent that are headers.
For a long message, n is about M ÷ P, so the overhead tends to H ÷ (P + H) = 40 ÷ 1500, about 2.7%. Short messages waste more, because their last packet is nearly empty.
Worked example 5: the speed of light
A signal travels through fibre at about 200,000 km per second. How long does it take to cross the 5,500 km from London to New York, and to get a reply?
| Working | Reason |
|---|---|
| Time = 5500 ÷ 200000 = 0.0275 s | Distance divided by speed |
| That is 27.5 ms each way | Multiply the seconds by a thousand |
| A request and reply take at least 55 ms | The signal must go there and back |
| No faster link can beat this | It is limited by the speed of light in glass |
Check yourself. Why might packets from the same message arrive in a different order from the one they were sent in?
Show the answer
Each packet is routed independently, so they may take different routes of different lengths through the network. A packet on a quicker route can overtake one sent before it.
Section 3
Protocols and the TCP/IP stack
Protocols
A protocol is a set of rules for communication: what messages look like, what order they come in and what to do when something goes wrong. Two devices can only communicate if they follow the same protocols. A standard protocol lets equipment from different manufacturers work together, which is why a phone made by one company can load a website run on servers made by another.
Layers
The protocols of the internet are arranged in four layers, called the TCP/IP stack. Each layer does one job, and uses the services of the layer below it without needing to know how that layer works.
A useful picture is sending a letter through a company's post room. The writer only writes the letter. A clerk puts it in an envelope and addresses it. The post room adds a stamp and sorts it into a sack for the right town, and the van driver only cares which depot the sack goes to. Each person adds their own wrapping and none needs to understand the others' jobs, and the receiving end unwraps in reverse order.
The application layer provides services to programs, such as HTTP for web pages, SMTP for sending email, IMAP for reading it and FTP for transferring files. The transport layer, using TCP, splits data into packets, numbers them, checks they all arrive and asks for any missing ones to be resent. UDP is a simpler transport protocol that sends packets without checking, which suits live video, where a late packet is useless anyway.
The transport layer also uses port numbers, which say which program on a computer the data is for. A web server listens on port 80 for HTTP and port 443 for HTTPS, and an email server on its own ports. An IP address finds the computer; the port number finds the program on it. Together they are called a socket.
TCP begins every connection with a short exchange of messages, often called a handshake. The client asks to connect, the server agrees, and the client confirms, so both sides know the other is ready and agree on the sequence numbers to use. Only then does the data start to flow. UDP skips all of this, which is another reason it is quicker for live traffic.
The internet layer, using IP, adds the source and destination IP addresses and routes each packet across networks. The link layer handles the physical network, such as Ethernet or Wi-Fi. It adds the MAC addresses of the next device on the path and turns the data into electrical, light or radio signals.
Common mistake: thinking each layer must know how the layers below it work. Layers are independent. A web browser using HTTP neither knows nor cares whether its packets travel over Wi-Fi, Ethernet or fibre. That independence lets each layer be changed or improved without rewriting the others.
Encapsulation
On the sending computer, data passes down the stack. Each layer adds its own header in front of the data it receives from the layer above, a process called encapsulation. On the receiving computer, the data passes up the stack, and each layer removes and reads its own header before passing the rest up.
Worked example 6: headers on the way down
A browser sends 1,000 bytes of HTTP data. TCP adds a 20-byte header, IP a 20-byte header and Ethernet an 18-byte header and trailer. How big is the unit at each layer?
| Working | Reason |
|---|---|
| Application: 1,000 bytes | The HTTP data itself |
| Transport: 1000 + 20 = 1,020 bytes | TCP adds its header |
| Internet: 1020 + 20 = 1,040 bytes | IP adds the addresses |
| Link: 1040 + 18 = 1,058 bytes | Ethernet adds its frame |
Other protocols
Many other protocols do particular jobs. HTTPS is HTTP with encryption, so web pages and form data cannot be read by anyone listening. POP3 downloads email to one device, while IMAP keeps it on the server so it can be read from several devices. DHCP gives each device an IP address automatically when it joins a network.
Protocols are agreed by standards bodies, groups of engineers from many companies and countries who publish each protocol openly. Anyone can then build software or hardware that follows it. Open standards are why the internet grew so quickly: no company had to ask permission, or pay, to build something that worked with everything else.
The advantages of layering are the same as those of decomposition in programming. Each layer is simpler to design and test on its own. Manufacturers can build hardware for one layer, such as a Wi-Fi card, knowing it will work with every program above it. And a new protocol can replace an old one in one layer, as HTTPS replaced HTTP, without changing the rest.
Check yourself. In the TCP/IP stack, which layer makes sure all the packets of a file arrive, and which protocol does it use?
Show the answer
The transport layer, using TCP. It numbers the packets, checks they have all arrived, and asks for any missing or damaged ones to be sent again.
Section 4
IP addresses and subnets
IPv4 addresses
Every device on the internet has an IP address. An IPv4 address is 32 bits long, written as four numbers from 0 to 255, separated by dots, such as 192.168.1.10. Each number is one byte, so the address can be written in binary as four groups of eight bits.
A 32-bit address allows only about 4.3 billion different addresses, which is too few for every device in the world. IPv6 uses 128-bit addresses, written in hexadecimal, which allows an enormous number. Many home and school networks also use private addresses, such as those starting 192.168, behind a single public address shared through the router.
A router that shares one public address among many private ones is doing network address translation. It rewrites the source address of every outgoing packet to the public one. It also remembers which device each conversation belongs to, so it can pass the replies back to the right place. Private addresses can be reused by every home and school, which has stretched the supply of IPv4 addresses for many years.
Devices usually get their IP addresses automatically. When a laptop joins a school network, it asks a DHCP server for an address. The server lends it one from a pool for a set time, along with the subnet mask, the address of the router and the address of a DNS resolver. When the laptop leaves, the address goes back into the pool for another device.
Network and host parts
An IP address has two parts. The first bits identify the network, and the rest identify the device, or host, on that network. A subnet mask, or a prefix length written after a slash, says where the split comes. In 192.168.1.10/24, the first 24 bits are the network part, matching the mask 255.255.255.0.
Applying the mask, by ANDing each bit of the address with the mask, gives the network address, with every host bit set to 0. Two devices are on the same network if they have the same network address. Devices on the same network can send to each other directly; packets for any other network must go through a router.
Splitting a large network into subnets has benefits beyond saving addresses. A school might give each building, or each kind of device, its own subnet. Traffic within a subnet stays there, so the network is less congested. The router between subnets can also control which kinds of traffic may cross, which improves security.
Worked example 7: binary and the network address
Write 192.168.1.10 in binary, and find its network address with a /24 prefix.
| Working | Reason |
|---|---|
| 192 = 11000000, 168 = 10101000 | Each number is one byte |
| 1 = 00000001, 10 = 00001010 | Pad every byte to eight bits |
| The first 24 bits are the network part | The prefix length is 24 |
| Network address = 192.168.1.0 | The host bits are set to 0 |
Usable hosts
Working out a subnet by hand always follows the same steps. Find the host bits and the block size, find which block the address falls in, then read off the first and last addresses of that block. Within a subnet, two addresses are reserved. The address with every host bit 0 names the network itself. The address with every host bit 1 is the broadcast address, which sends to every device on the network at once. So a subnet with h host bits has 2^h addresses but only 2^h − 2 that can be given to devices.
Figure 10.2 shows two addresses on a network. Change the prefix length and watch which bits belong to the network, whether the two devices share a network and how many hosts it holds. Then switch on the wrong model.
Subnets
- Usable hosts
- 254
- Same network?
- yes
- Network of 192.168.1.200
- 192.168.1.0
Common mistake: thinking every address in the block can be given to a device. The first address of a subnet names the network and the last is its broadcast address. Neither can be given to a device, so a block of 2^h addresses has only 2^h − 2 usable ones.
Worked example 8: a /26 subnet
Find the network address, broadcast address and number of usable hosts for 192.168.1.200/26.
| Working | Reason |
|---|---|
| Host bits = 32 − 26 = 6, so blocks of 64 | Each subnet holds 2⁶ addresses |
| 200 lies in the block 192 to 255 | 192 = 3 × 64 is the start of its block |
| Network 192.168.1.192, broadcast 192.168.1.255 | The first and last addresses of the block |
| Usable hosts = 64 − 2 = 62 | Two addresses are reserved |
Worked example 9: the same network?
Are 192.168.1.10 and 192.168.1.200 on the same network with a /24 prefix? And with a /25 prefix?
| Working | Reason |
|---|---|
| /24: both network addresses are 192.168.1.0 | The last byte is all host bits |
| So with /24 they are on the same network | Their network parts match |
| /25: 10 is in 0 to 127, but 200 is in 128 to 255 | The 25th bit differs |
| So with /25 they are on different networks | A router is needed between them |
Derivation: the size of a subnet
With a prefix of p bits, the host part has h = 32 − p bits. Each host bit can be 0 or 1, so the block holds
The all-zeros host part names the network and the all-ones host part is the broadcast address, so the usable hosts are
With p = 24, H = 256 − 2 = 254. Each extra bit of prefix halves the block, so /26 gives 62 and /30 gives just 2, enough for a link between two routers.
Check yourself. How many usable hosts does a /20 network have?
Show the answer
It has 32 − 20 = 12 host bits, so 2¹² = 4,096 addresses. Removing the network and broadcast addresses leaves 4,094 usable hosts.
Section 5
The Domain Name System
Names and addresses
People find names far easier to remember than numbers, so websites are reached by domain names, such as a school's address ending .sch.uk. Computers need the IP address to send packets. The Domain Name System, or DNS, is a worldwide, distributed directory that translates domain names into IP addresses.
A domain name is read from right to left. The top-level domain comes last, such as .uk or .com. The part before it is the second-level domain, and further parts name particular servers or sections. A uniform resource locator, or URL, adds the protocol and the path to a particular page, such as https://, then the domain name, then /admissions/apply.
Resolving a name
When a browser needs an IP address, it first checks its own cache of recent lookups, then the computer's. If neither has it, it asks a DNS resolver, usually run by the internet service provider. The resolver asks a root server, which points it to the servers for the top-level domain. Those point it to the domain's own authoritative server, which gives the IP address. The resolver passes the address back to the browser and keeps a copy for next time.
Common mistake: thinking the DNS server stores the website. A DNS server only stores names and the IP addresses they point to. Once the browser has the address, it connects directly to the web server, which holds the pages. DNS is a directory, not a host.
Caching
DNS lookups would be slow if every one went all the way to a root server. So every resolver and computer caches the answers it gets, for a time set by the domain's owner called the time to live. Most lookups are answered from a cache within a few milliseconds.
Caching has a cost: if a website moves to a new IP address, some users may be sent to the old address until their cached copy expires. So a site that plans to move lowers its time to live beforehand, so that caches check for changes often, then raises it again once the move is complete.
Worked example 10: the effect of caching
A resolver handles 1,000 lookups an hour, and 90% are answered from its cache. How many lookups an hour must go out to other DNS servers?
| Working | Reason |
|---|---|
| Cache hits = 1000 × 0.9 = 900 | These need no further lookup |
| Misses = 1000 − 900 = 100 | Only these go out to other servers |
| Outside lookups are cut by 90% | Caching saves time and network traffic |
| Popular sites are almost always cached | They are looked up constantly |
Because DNS is distributed across many servers, no single server holds every name, and the system keeps working if some servers fail. There are many copies of every root server around the world, so a lookup never depends on one machine in one place. Each organisation manages the names in its own domain, which spreads the work of keeping the directory up to date.
Domain names are registered through companies that pass the details to the organisation running each top-level domain. The owner of a domain can then create names within it, such as a separate name for its email server. Each name can point at whatever IP address the owner likes. Moving a website to a new server then means changing one DNS record, not telling every visitor a new number.
Check yourself. After DNS has given a browser an IP address, which server does the browser connect to for the web page?
Show the answer
It connects to the web server at that IP address. The DNS servers only provided the address; the pages themselves are stored on, and sent by, the web server.
Section 6
Client–server and peer-to-peer networks
Client–server networks
In a client–server network, servers provide services and clients request them. A school's file server stores everyone's work, its email server handles messages, and its web server hosts the website. Clients, such as pupils' laptops, send requests and receive responses.
A single physical computer can run several servers at once, and one service can be spread across thousands of computers. What makes something a server is its role: it waits for requests and answers them. A pupil's laptop is a client of the school's file server, but the same laptop could act as a server if it shared a folder with a friend.
Central servers make the network easy to manage. Files are backed up in one place, security settings are applied to everyone, and software can be updated centrally. But the servers are expensive, need skilled staff, and can become a bottleneck or a single point of failure if many clients make requests at once.
Peer-to-peer networks
In a peer-to-peer network, every device, or peer, is equal and can act as both client and server. Peers share files and resources directly with each other. A peer-to-peer network is cheap and simple to set up for a few devices, and it has no single server that can fail.
Large peer-to-peer systems are used to share big files with many people. A file is split into pieces, and each peer downloads pieces from many others while uploading the pieces it already has. The more peers there are, the more upload capacity the system has, so it copes well with demand. The drawbacks are that management and backup are harder, security is weaker, and the same methods can be used to share files illegally.
Choosing between the two depends on what the network is for. An organisation that must control who sees which files, keep backups and apply security rules needs servers. A small group sharing a printer, or a system that must spread a large file to huge numbers of people cheaply, suits peer-to-peer. Many real systems use servers for control and peer-to-peer methods for bulk delivery.
Cloud computing is a form of client–server computing on a huge scale. Organisations rent processing and storage in a provider's data centres instead of running their own servers. They can add capacity in minutes when demand rises and pay only for what they use. But they depend on the provider and on their internet connection, and their data is stored on someone else's machines.
Common mistake: thinking peer-to-peer means every peer must hold every file. Each peer holds only some of the data, and shares the pieces it has. A peer downloading a large file gathers different pieces from different peers at once, which is what makes the method fast.
Worked example 11: one server, many clients
A server with an upload speed of 100 Mbps must send an 800-megabit file to 100 clients. How long does it take?
| Working | Reason |
|---|---|
| Total to send = 100 × 800 = 80,000 megabits | Every client needs its own copy |
| Time = 80000 ÷ 100 = 800 s | All of it goes through the server's upload |
| That is about 13 minutes | The server's link is the bottleneck |
| Doubling the clients doubles the time | The server does all the work |
Worked example 12: sharing peer to peer
The same 100 machines now also upload to each other, each at 10 Mbps, while the server still uploads at 100 Mbps. What is the least time to share the file?
| Working | Reason |
|---|---|
| Upload capacity = 100 + 100 × 10 = 1,100 Mbps | Peers add their uploads to the server's |
| Time ≥ 80000 ÷ 1100 = 72.7 s | The same 80,000 megabits must be uploaded |
| The server must still send one copy: 800 ÷ 100 = 8 s | 72.7 s is the larger limit |
| Peer to peer takes about 73 s against 800 s | Extra peers bring extra capacity |
Derivation: how long it takes to share a file
A file of F megabits must reach n machines. A server alone, uploading at u megabits per second, must send n copies.
Peer to peer, every machine also uploads at p. The server must send at least one copy, and all n copies must be uploaded by the server and peers together, so
As n grows, nF ÷ (u + np) approaches F ÷ p, so the time stays roughly constant however many peers join, while the server's time grows in proportion to n.
In practice, many services combine the two. A video streaming service runs huge client–server systems, but copies its films to servers close to its viewers. This content delivery network means no single server carries every request.
Check yourself. A server sends a file to 10 clients in 80 seconds. With peer-to-peer sharing, the least time is 40 seconds. Why is the saving smaller than with 100 clients?
Show the answer
With only 10 peers, there is little extra upload capacity to add to the server's: the server alone takes 80 seconds, and peer-to-peer at least 40. The peer-to-peer advantage grows with the number of peers, because each new peer brings capacity as well as demand.
Reference
Key algorithms and definitions
| Term or algorithm | What it means | Example | Cost or rule to learn |
|---|---|---|---|
| LAN and WAN | Small local network; wide area network | A school; the internet | WAN links are usually rented |
| Star and mesh | Central switch; many routes | Office star; internet mesh | Full mesh needs n(n − 1) ÷ 2 links |
| Switch and router | Within a network; between networks | MAC against IP addresses | Router forwards packets |
| Packet switching | Packets routed independently | 4 packets for 5,000 bytes | Bytes sent = M + nH |
| TCP/IP stack | Application, transport, internet, link | HTTP, TCP, IP, Ethernet | Each layer adds a header |
| IPv4 address | 32 bits as four bytes | 192.168.1.10 | About 4.3 billion addresses |
| Subnet | Network bits and host bits | /24 = 255.255.255.0 | 2^(32 − p) − 2 hosts |
| DNS | Names to IP addresses | Root, TLD, domain servers | Answers are cached |
| Client–server | Central servers, requesting clients | A school file server | Time nF ÷ u |
| Peer-to-peer | Equal peers share pieces | Large file sharing | Time max(F ÷ u, nF ÷ (u + np)) |
Programming task
Programming task: a subnet calculator
Aim
Write a Python program that takes an IPv4 address and a prefix length. It must print the address in binary, the subnet mask, the network and broadcast addresses and the number of usable hosts. It must also say whether a second address is on the same network.
Design
The program converts the address into a single 32-bit integer, by multiplying up the four bytes. The block size is 2 to the power of the host bits. Dividing the address by the block size and rounding down, then multiplying back, gives the network address; adding the block size minus one gives the broadcast address. Converting back to four bytes uses division and the MOD operator.
Two addresses are on the same network if their network addresses are equal. The number of usable hosts is the block size minus two, never less than zero.
Code
def to_int(ip):
a, b, c, d = (int(part) for part in ip.split("."))
return ((a * 256 + b) * 256 + c) * 256 + d
def to_ip(n):
return ".".join(str(n // 256 ** k % 256) for k in (3, 2, 1, 0))
def subnet(ip, prefix):
size = 2 ** (32 - prefix)
network = to_int(ip) // size * size
return {
"binary": ".".join(format(int(p), "08b") for p in ip.split(".")),
"mask": to_ip(2 ** 32 - size),
"network": to_ip(network),
"broadcast": to_ip(network + size - 1),
"hosts": max(0, size - 2),
}
def same_network(a, b, prefix):
return subnet(a, prefix)["network"] == subnet(b, prefix)["network"]
print(subnet("192.168.1.200", 26))
print(same_network("192.168.1.10", "192.168.1.200", 24))
Working with one integer makes the arithmetic simple: the network address is just the address rounded down to a multiple of the block size. The mask is the full 32-bit range minus the block size, which leaves ones in every network bit.
Testing
| Test | Input | Expected output | Why this test |
|---|---|---|---|
| 1 | 192.168.1.10/24 | network 192.168.1.0, 254 hosts | Matches worked example 7 |
| 2 | 192.168.1.200/26 | network 192.168.1.192, broadcast 192.168.1.255, 62 hosts | Matches worked example 8 |
| 3 | 192.168.1.10 and 192.168.1.200, /24 | True | Matches worked example 9 |
| 4 | 192.168.1.10 and 192.168.1.200, /25 | False | The 25th bit differs |
| 5 | 10.20.37.5/20 | network 10.20.32.0, broadcast 10.20.47.255, 4,094 hosts | A prefix that splits the third byte |
| 6 | 172.16.45.130/28 | network 172.16.45.128, 14 hosts | A small subnet |
| 7 | 192.168.1.1/30 | 2 hosts | The smallest useful subnet |
Evaluation
The calculator gives the right results for every test, including prefixes that split a byte, as in tests 5 and 6, which are the hardest to do by hand. Test 7 shows the smallest practical subnet, used for a link between two routers.
The program does not check its input, so an address such as 300.20.40.60 or a prefix of 40 would give nonsense. An improvement would validate each byte and the prefix, and would add support for IPv6 addresses.
Practice
Practice ladder
Rung 1: Recall and fluency
Q1. Compare a LAN with a WAN.
Worked solution to Q1
A LAN covers a small area, such as one building, and its equipment is usually owned by the organisation. A WAN covers a large area, such as a country, and usually uses links rented from telecommunications companies.
Q2. State what is held in the header of a packet.
Worked solution to Q2
The header holds the source and destination IP addresses and a sequence number showing where the packet fits in the message. It also holds a limit on the number of hops, and a checksum used to detect errors.
Q3. Name the four layers of the TCP/IP stack, from the top.
Worked solution to Q3
From the top, the layers are the application layer, the transport layer, the internet layer and the link layer. Data passes down them when sent and up them when received.
Rung 2: Application
Q4. A 10,000-byte file is sent in packets of up to 1,460 data bytes with 40-byte headers. Find the packets and the bytes sent.
Worked solution to Q4
Dividing gives 10000 ÷ 1460 = 6.85, which rounds up to 7 packets. The headers add 7 × 40 = 280 bytes, so 10,000 + 280 = 10,280 bytes are sent in total.
Q5. Find the network address and usable hosts for 172.16.45.130/28.
Worked solution to Q5
There are 4 host bits, so the addresses come in blocks of 16. The last byte, 130, lies in the block from 128 to 143, so the network address is 172.16.45.128. There are 16 − 2 = 14 usable hosts.
Q6. Write 172.16.45.130 in binary.
Worked solution to Q6
Convert each byte on its own, using the place values from 128 down to 1. For example, 172 = 128 + 32 + 8 + 4, which gives 10101100, and 130 = 128 + 2, which gives 10000010. Doing the same for 16 and 45 gives 10101100.00010000.00101101.10000010.
Q7. Count the links in a full mesh of 8 devices.
Worked solution to Q7
Each device links to the other 7, which counts every link twice. So the number of links is 8 × 7 ÷ 2 = 28, compared with 8 for a star of the same devices.
Rung 3: Analysis
Q8. Explain why live video calls often use UDP rather than TCP.
Worked solution to Q8
TCP waits for missing packets to be resent, which causes delays and freezing. In a live call, a late packet is useless anyway, so UDP's simple sending without checking keeps the call smooth, with only an occasional small glitch.
Q9. Explain why a router is needed between 192.168.1.10 and 192.168.1.200 when the prefix is /25.
Worked solution to Q9
With /25, the first address is in the block from 0 to 127 and the second in the block from 128 to 255, so they have different network addresses. Devices on different networks can only reach each other through a router.
Q10. Using the network of routers in this chapter, explain what happens to packets from A to B if the link from R4 to R3 fails.
Worked solution to Q10
The best route used R4 to R3, so the routers recalculate. The next fastest route is A, R1, R3, B, with a total delay of 4 + 5 + 2 = 11 ms, so packets now take that route instead.
Q11. A resolver handles 5,000 lookups an hour, and 96% are answered from its cache. How many go out to other servers?
Worked solution to Q11
The cache answers 5,000 × 0.96 = 4,800 lookups. The other 5,000 − 4,800 = 200 must be sent on to other DNS servers, a small fraction of the total.
Rung 4: Stretch
Q12. Show that the overhead of headers tends to H ÷ (P + H) for long messages, and find it for P = 1,460 and H = 40.
Worked solution to Q12
For a long message, n is close to M ÷ P, so the overhead nH ÷ (M + nH) becomes (MH ÷ P) ÷ (M + MH ÷ P). Cancelling M and multiplying by P gives H ÷ (P + H) = 40 ÷ 1500, about 2.7%.
Q13. A server uploading at 100 Mbps sends an 800-megabit file to 1,000 clients. Compare the time with peer-to-peer sharing at 10 Mbps per peer.
Worked solution to Q13
The server alone needs 1,000 × 800 ÷ 100 = 8,000 s. Peer to peer, the capacity is 100 + 1,000 × 10 = 10,100 Mbps, so it takes 800,000 ÷ 10,100, about 79 s, a hundred times faster.
Q14. Explain why the internet was designed with packet switching rather than circuit switching.
Worked solution to Q14
Packet switching lets many conversations share every link, using capacity only while packets cross it. It also survives failures, because packets are routed around broken links, which suited a large network with no central control.
Exam practice
Exam technique and worked answer workshop
For hardware questions, say what a device does and what it uses to do it. "A router forwards packets between networks using their destination IP addresses" earns marks; "a router connects to the internet" earns little.
For packet calculations, round the number of packets up, and remember that every packet has a header. For subnets, find the host bits first, then the block size, then the network and broadcast addresses at the ends of the block.
For the TCP/IP stack, name each layer, give a protocol it uses and say what it adds or does. Describe encapsulation as each layer adding its own header on the way down and removing it on the way up.
Exam question 1 (4 marks · Paper 01 · about 5 minutes)
(a) Compare the jobs of a switch and a router. [2]
(b) Give one advantage and one disadvantage of a star topology. [2]
Mark scheme for exam question 1
- B1 a switch connects devices within one network, using MAC addresses
- B1 a router connects different networks and forwards packets between them using IP addresses
- B1 advantage: if one cable fails only that device is affected, or devices are easy to add
- B1 disadvantage: if the central switch fails the whole network fails, or more cable is needed
Exam question 2 (5 marks · Paper 01 · about 7 minutes)
A 5,000-byte message is sent in packets carrying up to 1,460 bytes of data, each with a 40-byte header.
(a) Calculate the number of packets needed. [1]
(b) Calculate the total number of bytes sent. [2]
(c) Explain how the receiving computer rebuilds the message. [2]
Mark scheme for exam question 2
- B1 4 packets
- M1 4 × 40 = 160 bytes of headers
- A1 5,160 bytes
- B1 it uses the sequence numbers to put the packets back in order
- B1 it requests any missing or damaged packets to be resent
Exam question 3 (6 marks · Paper 01 · about 9 minutes)
A device has the address 192.168.1.200 on a network with the prefix /26.
(a) State the subnet mask. [1]
(b) Calculate the network address and the broadcast address. [3]
(c) Calculate the number of devices the network can hold. [2]
Mark scheme for exam question 3
- B1 255.255.255.192
- M1 6 host bits, so blocks of 64 addresses
- A1 network address 192.168.1.192
- A1 broadcast address 192.168.1.255
- M1 2⁶ − 2
- A1 62
Exam question 4 (5 marks · Paper 01 · about 7 minutes)
Describe how the layers of the TCP/IP stack work together when a web page is requested.
Mark scheme for exam question 4
- B1 the application layer uses HTTP or HTTPS to form the request
- B1 the transport layer uses TCP to split the data into numbered packets and check they arrive
- B1 the internet layer adds the source and destination IP addresses and routes the packets
- B1 the link layer sends the packets over the physical network, such as Ethernet or Wi-Fi
- B1 each layer adds its own header on the way down, and the receiver removes them on the way up
Exam question 5 (6 marks · Paper 01 · about 9 minutes)
A games company must deliver a 4 GB update to two million players on the day it is released. Discuss whether it should use a client–server or a peer-to-peer approach.
Mark scheme for exam question 5
Level 3 (5–6 marks): a balanced comparison of both approaches, linked to the size of the update and the number of players, covering capacity, cost, control, security and reliability. The answer reaches a justified recommendation. It is well organised and uses technical terms accurately.
Level 2 (3–4 marks): a correct description of both approaches with some link to the update. The comparison or the recommendation is limited.
Level 1 (1–2 marks): some relevant points, such as describing a server, with little application. The answer may lack structure.
0 marks: no response or no response worthy of credit.
Indicative content:
- client–server: every player downloads from the company's servers
- the company controls the files, so they cannot be tampered with, and security is easier
- but two million downloads of 4 GB at once need enormous server capacity, which is expensive
- the time to serve everyone grows in proportion to the number of players
- peer-to-peer: players share pieces of the update with each other
- every player who downloads also uploads, so capacity grows with demand
- it is much cheaper for the company, but uses players' own upload bandwidth
- the company must check the pieces, for example with hashes, so nobody can insert malware
- a content delivery network is a middle way, with copies of the update on servers near the players
- recommendation: peer-to-peer with hash checks, or a CDN, rather than a single set of servers
Annotated model answer: exam question 5
"With client–server, every player downloads the update from the company's servers. The company controls the files and can secure them. But two million downloads of 4 GB on one day need enormous upload capacity." (Client–server, with a benefit and a cost.)
"The time for a server to send every copy grows in proportion to the number of players. With peer-to-peer, each player uploads pieces to others as they download, so the capacity grows with the number of players." (The key comparison of capacity.)
"Peer-to-peer is much cheaper for the company, but it uses players' upload bandwidth. The pieces must be checked with hashes so nobody can slip in malware." (Drawbacks and how to manage them.)
"So the company should use peer-to-peer with hash checks, or a content delivery network, rather than relying on its own servers alone." (A justified recommendation.)
This answer reaches Level 3 and scores 6 marks. It weighs both approaches against the scale of the update and reaches a clear recommendation.
Exam question 6 (4 marks · Paper 01 · about 5 minutes)
Describe how the Domain Name System finds the IP address of a website the first time it is visited.
Mark scheme for exam question 6
- B1 the browser and computer check their caches, then ask a DNS resolver
- B1 the resolver asks a root server, which refers it to the top-level domain's servers
- B1 those refer it to the domain's authoritative server, which returns the IP address
- B1 the address is passed back to the browser and cached for future lookups
Exam question 7 (4 marks · Paper 01 · about 5 minutes)
(a) Contrast packet switching with circuit switching. [2]
(b) Give one advantage of packet switching. [1]
(c) Give one advantage of circuit switching. [1]
Mark scheme for exam question 7
- B1 packet switching sends each packet independently, by whatever route is best at the time
- B1 circuit switching sets up a dedicated path for the whole communication before sending
- B1 links are shared efficiently, or packets can be routed around failures
- B1 data arrives in order at a steady rate, which suits live voice
Exam question 8 (6 marks · Paper 02 · about 9 minutes)
Write a function packetsAndBytes(messageBytes, payload, header) that returns the number of packets needed and the total bytes sent. State what it returns for a 20,000-byte message with a payload of 1,460 bytes and a 40-byte header.
Mark scheme for exam question 8
- B1 a function header with the three parameters
- B1 the number of packets found by dividing the message size by the payload
- B1 the result rounded up, for example by adding 1 when there is a remainder
- M1 the total found as the message size plus packets × header
- B1 both values returned
- A1 14 packets and 20,560 bytes
Annotated model answer: exam question 8
function packetsAndBytes(messageBytes, payload, header)
packets = messageBytes DIV payload
if messageBytes MOD payload > 0 then
packets = packets + 1
endif
total = messageBytes + packets * header
return packets, total
endfunction
The header earns the first mark and the division the second. Adding one packet when there is a remainder rounds up and earns the third. Adding a header per packet earns the method mark, returning both values earns the fifth, and 14 packets and 20,560 bytes earn the last.
Connections
Synoptic link
Routers find the fastest route with shortest-path algorithms such as Dijkstra's, from Graph traversal and shortest paths. Applying a subnet mask is a bitwise AND, from Number systems and binary arithmetic and Boolean algebra and logic circuits.
Protecting data as it crosses networks is covered in Network security and threats. The protocols that deliver web pages, and the way search engines find them, are in Web technologies and search engines. The checksums in packet headers are a simple form of the hashing in Compression, encryption and hashing.
Synoptic question 1. A router receives a packet for 192.168.1.77 and has a route for 192.168.1.64/26. Does the route apply? Show the AND.
Answer to synoptic question 1
With /26 the mask's last byte is 11000000. ANDing 77, which is 01001101, gives 01000000, which is 64. So the network address is 192.168.1.64, and the route applies.
Recap
Recap and next step
- LANs cover small areas and WANs large ones. Star topologies use a central switch; mesh topologies give many routes. Switches work within a network and routers between networks.
- Packet switching sends packets independently by the best route; bytes sent = M + nH, with n = ⌈M ÷ P⌉.
- The TCP/IP stack has application, transport, internet and link layers, and each adds its own header in encapsulation.
- An IPv4 address is 32 bits; the prefix splits network and host bits, and a subnet has 2^(32 − p) − 2 usable hosts.
- DNS resolves names to IP addresses through resolvers, root, top-level domain and authoritative servers, with caching.
- Client–server networks centralise control; peer-to-peer networks grow their capacity with every peer.
Next chapter: Network security and threats looks at how networks are attacked and defended, from malware and phishing to firewalls, proxies and encryption.
For your exam board
OCR A Level Computer Science (H446): this chapter covers section 1.3.3, Networks, in Component 01: characteristics of networks and the importance of protocols and standards, the TCP/IP stack, DNS, protocol layering, LANs and WANs, packet and circuit switching, client–server and peer-to-peer networks.
AQA A Level Computer Science (7517): the fundamentals of communication and networking section covers network topology, client–server and peer-to-peer networking, the TCP/IP stack, IP addresses, subnet masks and the Domain Name System, assessed on Paper 2.