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

Free lesson

IP addresses and ports

After this lesson: I can say what an IP address and a port are.

45 min lessonNo commands

Is it the address or the port?

A support ticket arrives: "The order page will not load." Your team lead reads it and asks one question. "Is it the address or the port?"

Every message on a network needs two numbers to arrive. One finds the computer. The other finds the program on it. In this lesson you learn to read both.

By the end of this lesson

Note

I can say what an IP address and a port are.

Key words

WordWhat it meansExample
Servera computer that answers requests
Clientthe program that sends a request
IP addressthe number that names a device on a network192.168.1.23
Portthe number that names one program on a device443
Socketan IP address and a port together203.0.113.10:443
IPv6the newer, longer kind of IP address2001:db8::10

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

An IP address names a device

Every device on a network has an IP address. Laptops, phones, printers and servers all have one.

The common kind, IPv4, is four numbers with dots between them, like 192.168.1.23. Each number is from 0 to 255.

IPv6 addresses are longer and use letters too, like 2001:db8::10. They exist because the world ran short of IPv4 addresses.

Worked example: one small office

DeviceIP addressWhat it does
Sales laptop192.168.1.23Sends requests
Office printer192.168.1.40Takes print jobs
Router192.168.1.1Links the office to the internet
Shop web server203.0.113.10Answers order requests

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

Try it: check an address

Code: python
address = "192.168.1.23"

# split cuts the text at every dot and gives a list of four parts
parts = address.split(".")
print(parts)

for part in parts:
    number = int(part)
    if number > 255:
        print(part, "is too big: each part is 0 to 255")
    else:
        print(part, "is fine")

# Change the address to "192.168.1.300", then press Run.

A port names a program

One server often runs several programs. It might run a website, a database and a remote login service at the same time.

A port is a number from 1 to 65535. It tells the server which program should get the message. Some numbers are used so widely that everybody knows them. A web server using HTTPS listens on port 443.

Ports you will meet at work

PortServiceUsed for
22SSHLogging in to a server remotely
53DNSTurning names into addresses
80HTTPWeb pages, not encrypted
443HTTPSWeb pages, encrypted
5432PostgreSQLA database

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

A common mistake

Warning

Many people think a port is the socket on the back of a computer. Here a port is a number, not a hole for a cable. It lives in the message, and it tells the device which program should read it.

Try it: look up a port

Code: bash
# A small copy of the list of well-known ports
printf "ssh 22\ndns 53\nhttp 80\nhttps 443\npostgres 5432\nmysql 3306\n" > ports.txt

# Which service listens on port 443?
grep " 443$" ports.txt

# Change 443 to 22, then press Run.

Both ends have an address and a port

A socket is an address and a port written together: 203.0.113.10:443.

A connection has a socket at each end. The server's port is fixed and well known. The client picks a high port at random for each connection. That is how one laptop can have ten browser tabs open to the same shop.

Worked example: one connection

EndIP addressPortWritten as
Client: sales laptop192.168.1.2351544192.168.1.23:51544
Server: shop website203.0.113.10443203.0.113.10:443

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

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.