# What would be a good analogy for IP addresses and ports?

What would be a good way to describe ports? I learned about it using this analogy, but it seems pretty inadequate to me:

IP addresses are like street addresses of apartment buildings, and ports are like the apartments therein.

IP addresses are not segmented as a street address is, and second, apartments don't seem at all analogous to ports in terms of behavior.

• I don't know how many times I've seen the IP address compared to a phone number, with the DNS being the phone book. To add ports to that, just add extensions to the phone number. Extensions that are standardized so you always know which extension to connect to for some department. The web department is normally extension 80, the email department uses extension 25. If you have an encrypted phone then you used different extensions. Jul 2 '17 at 3:13
• Analogies are like shoes ... they do not always fit perfectly. Jul 2 '17 at 10:43
• The apartment street address vs. apartment number is the analogy I use, and it seems to work. Anything could work though - buildings and room numbers, classrooms and seats, pbx and extensions (this may add confusion) - as long as it references one destination with service dependent on who is contacted at that destination Jul 2 '17 at 19:41
• Phone numbers also have the name<->number mapping in form of telephone books (which may be a thing of the past though). Jul 7 '17 at 18:13
• why use an analogy? I would maybe use an analogy to explain it to someone not interested in computers to give them a quick idea of what it's for, but if you're actually teaching someone about IP addresses and ports, I don't see how an analogy helps, just tell them how it works. An analogy might actually be counter-productive, in fact. Jul 8 '17 at 3:08

I don't think the address metaphor is that off-the-mark -- real addresses are more segmented then IP addresses, true, but I don't know if that's really that much of a conceptual barrier.

Regarding ports, I would tweak your metaphor slightly -- a port is more like an apartment number, rather then the apartment itself. That is, the port just serves as an additional piece of information that's a part of your overall address, and can be used to route the mail to the appropriate apartment resident once your mail arrives.

(I guess to extend the metaphor, the people who live in the apartments are programs, and whoever sorts the mail at the apartment is like the operating system.)

What might also help is to explain that at the end of the day, a port is literally just a 16 bit int, nothing more, and that it's up to the receiving computer to do something reasonable with that number. If you emphasize that a port is just a number, it might make it a little easier for your students to independently derive what exactly you can and can't do with ports. After all, there are only so many things you can do with a single piece of metadata.

An alternative/complementary approach might be to focus less on metaphors and instead focus on motivating why we'd need ports in the first place. I'd personally start from the top down, and explain things like this:

1. Start by explaining how IP addresses are used to route packets.
2. Explain that once a computer finally receives a packet, we're still not done: the operating system need to figure out which currently-running program to give it to, if at all.

This might also be a good moment to ask your students to discuss and try coming up with a solution themselves -- hopefully, most of them will independently realize they need to include metadata of some kind. You can turn this into a mini-design lesson: an ideal solution should be simple, minimizes packet size, is future-proof, flexible, doesn't break the transport layer abstraction...

3. Tell them about ports, and explain that was the solution people ended up coming up with.

The hope is that focusing more on motivation will allow your students to acquire a reasonable conceptual model of how network protocols tend to work, even if they forget the exact details.

I think this approach also does a better job of answering the implicit "why?" question people have when learning new things. (Why ports? Why IP addresses? Why TCP? Why abstract things like this? Why is everything so complicated? etc.) Basically, confront it head-on by presenting things in a problem/motivation -> solution approach.

Caveat: this approach might just be a reflection of how I personally like learning things, so YMMV.

• Concurring: port is the apartment number, not the apartment. The apartment dweller has to do something with the delivered mail, akin to the program listening at that port. My success with the apartment building:number::IP:port analogy increased when I started showing pictures of a bank of mailboxes: sep.yimg.com/ay/yhst-14044557065217/… Jul 2 '17 at 4:17
• Why can we ping only houses, not appartments? In other words, a good metaphor would need good counterparts for TCP vs. UDP vs. IP. To begin with, there is in principle no relation between UDP port x and TCP port x (even if, e.g., DNS uses both UDP port 53 and TCP 53); even the fact that their number ranges are the same is not a principal necessity. My point is: Why try to use a metaphor at all to cover every aspect and detail, when pairs of numbers are a simple concept? Jul 2 '17 at 10:51

I used seats in the room.

The classroom has an address. So, if someone wanted to come and talk to one of the students they would come down the hall and into my room.

But, that only gets them to the room (server). They need to talk to a specific person, so they go to seat 17 so they can talk to Bob.

So Bob's socket is room.123:17.

I tried once to continue and the floor became cable runs, each hallway was a subnet, the corners were routers, and the school map was DNS. That pushed it a little too far :)

Did find though that most kids had at least a passing understanding of ports. Most of them had opened ports on their home routers to play games over the internet.

• Seats! Love it. The person in the seat is like the program registered with the OS to listen at that port. The analogy gets at the idea that programs at a port may or may not be the port's usual ilstener (students sit in the wrong seat), might not be running (absent student), and that programs might have assumptions they make about incoming packets. If a love note for Barb at 121:2 is sent to Alice at 121:1, who knows what Alice will do with it? Ignore it? Return receipt reporting incorrect delivery? Respond in a way that noone who knows Alice would have expected? Jul 2 '17 at 4:13
• @BennettBrown That's even better. Bob and Sue switched seats because there's a substitute and now Bob doesn't get his message. Jul 2 '17 at 14:33

You can consider an IP number to be analogous to a business's telephone number (reaching the main switchboard), and the port number to be the telephone extension number for one person.

This probably works better when there is no direct-dial to a specific person, and the main number is handled by an automated system.

• That's a nice point. Welcome to Computer Science Educators - I hope we hear more from you in the future!
– Ben I.
Jul 2 '17 at 13:32
• I've just noticed @Gypsy-Spellweaver 's comment to the original question, and realised that my answer is much the same.
– CSM
Jul 2 '17 at 13:45

I'd put a slight tweak on your analogy and go digital: email.

In email, your email address is your IP address. It comes in multiple parts, similar to IP blocks. There are many addresses under one domain, there are many domains under 1 TLD.

When a message is sent to an email address, it gets sent something pretended to the title, for example "Re" and "Fwd". That's similar to the port. They're all going to the same place, but it indicates something about the email that the computer can use to help process it.

As a former student I'd like to confess that for me these attempted analogies about the ports just made a simple thing sound mysterious and in retrospect had absolutely zero insight whatsoever.

What about saying instead that TCP/IP is just a program running on your computer like everything else and ports are just an integer in its configuration. That'd be a start.

• Might work. Might fail. Depends on how much experience the students have. Jul 3 '17 at 12:24

How about IP addresses being countries and ports being sea ports?

In the UK it works well because there are so many different sea ports that students recognise from looking at an online map of the British Isles.

You can extend the metaphor by talking about firewalls blocking off access to ports (e.g. sea blockades during WWII) or increasing security by removing external access (landlocked countries don't need to worry about sea invasion).

The main advantage over an address / flat number analogy is that you can extend it by talking about different types of ports (e.g. airports) for different protocols (e.g. UDP / TCP) with various implications for speed / reliability.

No analogy is perfect though - students who want to understand it fully should have experience using or hosting different services on the same server.

• This seems confusing. If a sea port like Liverpool is analogous to anything in networking I would expect it to be an interface, rather than a port.
– bdsl
Jul 2 '17 at 15:43
• I could see this going badly in parts of the US, where some students may be only dimly aware that there are such things as oceans (or other countries) Jul 3 '17 at 11:36

Using physical addresses is misleading - it would be clearer to use an email address, or a phone number.

If people still had POTS (telephone) in the house, you have segmented (area code) dynamic addresses, and 'mum, dad, youngest child etc.' which could represent ports - which are ways of reaching the same type of person in each house.

With this analogy, you can explain that people's phone number sometimes changes (but doesn't have to, even if they move location), and there are many different ways of addressing a set of people who might be in a house. Depending on who is at home, a different person might actually handle any specific call.

When someone places an outgoing call, you'd see which house the call was coming from, but not who (until they identify themselves).

• I don't understand. What are POTS? I mean, other than stovetop items.
– Ben I.
Jul 3 '17 at 15:14
• Is it clear enough with that edit? Jul 3 '17 at 15:16
• Yes, thank you :) I grew up with one (still have one, actually), but never knew it was called that.
– Ben I.
Jul 3 '17 at 15:18

Our department is small enough and close-knit enough that most of my students have seen the CS mail room across from the CS main office. A mail room that has a wall of slots, one for each faculty and staff in the department. I tweak the address metaphor and talk about how the mail slot is analogous to a port. Address (IP) gets the letter (packet) to the right building (computer), while the name on the address gets the letter (packet) to the right person (port/service).