Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

7.3 (6 of 8): Data packets

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

50 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

A long letter in many envelopes

You must post a long essay, but each envelope holds only one page. So you number the pages and put each in its own envelope, with the address.

The envelopes go by different routes. Some arrive late. The other person puts the pages back in order using the numbers. A network is a group of computers joined together. Networks send data in the same way. A packet is a small piece of data with an address on it. Each envelope is a packet.

Key words

WordWhat it meansExample
PacketA small, addressed piece of data sent over a network.One envelope
HeaderThe front part of a packet, with addresses and numbers for delivery.The address on the envelope
PayloadThe part of a packet that carries the real data.The page inside
TrailerThe end part of a packet, with a check value.A check number written on the back of the envelope
Packet switchingSending data as packets that may each take a different route.Envelopes on different roads
Sequence numberA number that puts packets back in the right order.Page 3 of 5
HopOne step of a packet from one router to the next.3 hops to the server
TTLTime to live: a count that drops at each router, and at zero the packet is thrown away.TTL 64
ProtocolA set of rules computers follow to talk to each other.HTTP
RedundancyExtra data added on purpose to help find mistakes.A check value
CRCCyclic redundancy check: a value that shows if data was damaged on the way.A number in the trailer
IP addressA number that names a device on a network.10.0.12.1
RouterA device that joins networks and sends packets on the right way.A home router
NetworkComputers joined to share data.The college network

The cells students fill in are left blank in this preview.

Match each packet part to its role

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

What is in a packet?

PartWhat it holdsRole
HeaderSource address, destination address, sequence numberGets the packet to the right place, in the right order
PayloadA piece of the dataThe reason the packet exists
TrailerAn error check value such as a CRCLets the receiver spot damaged data

The cells students fill in are left blank in this preview.

A packet from front to back

flowchart LR
  h["Header: source, destination, sequence number"] --> p["Payload: a piece of the data"] --> t["Trailer: CRC check value"]

Each packet can take its own route

Data is cut into packets. Each router reads the destination address and sends the packet on to the next router. Two packets of the same message can take different routes.

At the end, the receiver uses the sequence numbers to put the packets back in order. This helps: if one route is busy or broken, packets take another. Many people can share the same cables.

Try it: put the packets in order

Code: python
# Five packets arrive in the wrong order. Each has a sequence number.
arrived = {3: "my", 1: "I", 5: "phone", 2: "lost", 4: "new"}
for number in range(1, 6):
    if number in arrived:
        print(number, arrived[number])
    else:
        print(number, "is missing")

# Now delete one packet from the dictionary and run it again.
# The message has a gap: that is packet loss.

Make a free account to save your work and track your progress.

Extra: More in a real header

PartWhat it does
TTL (time to live)Goes down by one at each hop. At zero the packet is thrown away, so a lost packet cannot travel for ever.
ProtocolSays which set of rules the payload follows, such as TCP.
Where the parts really areThe IP header holds the addresses and the TTL. The sequence number is in the TCP header. The CRC is in the trailer of the frame.
Two more causes of lossThe TTL reaches zero, for example when packets go round in a circle. Or a router has no route to the network.

The cells students fill in are left blank in this preview.

Two routes, one message

flowchart LR
  s["Sender"] --> r1["Router 1"]
  r1 -->|"packets 1 and 3"| r2["Router 2"]
  r1 -->|"packets 2 and 4"| r3["Router 3"]
  r2 --> d["Receiver puts them in order"]
  r3 --> d

Extra: Send four packets and read them

Command
sudo ip netns exec h1 ping -c 4 -W 1 10.0.12.1

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: Send two packets that are lost

Command
sudo ip netns exec h1 ping -c 2 -W 1 10.0.3.2

A command for the lesson’s lab terminal. It is shown here and not run.

Extra: What ping showed

Note

In the first output each line has icmp_seq. This is the sequence number of the packet. Each line also has ttl, the time to live. The second output says every packet was lost, because the router has no route to that network.

Why packets are lost

There are two main causes of packet loss.

The first is congestion. Too many packets arrive and a router has no room, so it throws some away.

The second is errors. A cable or a device is faulty. Wi-Fi has interference (other signals that make yours worse). A damaged packet fails its check and is thrown away.

A lost packet is not always a big problem. The sender can send it again.

Match the cause of packet loss

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

A check value finds damaged data

How does the receiver know the data is not damaged? The sender works out a check value from the data and sends it too. The receiver works it out again. If the two values differ, the data changed.

Here is a simple check. Add up the digits. A real CRC is a stronger kind of check that computers work out very fast. You do not need to know how it works inside. You need to know that it finds damaged data.

Worked example: add the digits

StepSenderReceiver (good)Receiver (damaged)
Data4 7 24 7 24 8 2
Add the digits4 + 7 + 2 = 134 + 7 + 2 = 134 + 8 + 2 = 14
Check value sent131313
Same?not neededYes: accept itNo: 14 is not 13, so throw it away or ask again

The cells students fill in are left blank in this preview.

Worked example: the check for "Hi"

StepWhat to doResult
1Find the code of each letter. H is 72 and i is 105.72 and 105
2Add them: 72 + 105Check value 177
3The receiver gets "Ho". The code of o is 111. 72 + 111 = 183.183 is not 177: damaged

The cells students fill in are left blank in this preview.

Try it: a toy check

Code: python
data = "Hi"
check = 0
for letter in data:
    check = check + ord(letter)
print("Check value sent:", check)

received = "Ho"   # one letter changed on the way
again = 0
for letter in received:
    again = again + ord(letter)
print("Receiver works out:", again)
print("Damaged?", again != check)

Make a free account to save your work and track your progress.

Your turn: write the check

Write two functions. checksum(text) adds up the code of every character and gives back the total. In Python, ord("A") gives the code of a character, 65. is_damaged(text, check) gives back True when the text no longer matches the check number it came with. Press Run Tests to check.

Code: python
def checksum(text):
    # Give back a check number for the text.
    # Add up the code of every character.
    # Python gives you the code of a character with ord("A"), which is 65.
    return 0


def is_damaged(text, check):
    # Give back True if the text no longer matches the check number it came with.
    return False

Make a free account to save your work, or press Start lab to work through the whole lesson.

Extra: Keeping the check value small

A long message gives a very big total. A real check value has a fixed size, so it fits in the trailer.

One simple way: divide the total by 256 and keep only the remainder. Say the total is 366. 366 - 256 = 110, so the check value is 110. In Python the remainder is the % symbol: 366 % 256 is 110.

A real CRC also uses a special kind of division, on the bits.

Extra: Your turn: a check that stays small

Write the function small_checksum(text). It adds up the code of every character, like checksum. Then it gives back the remainder after dividing the total by 256. The symbol for the remainder is %. Press Run Tests to check.

Code: python
def small_checksum(text):
    # Add up the code of every character, with ord().
    # Then give back the remainder after dividing the total by 256.
    return 0

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Final check

Have a go

Final check

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.