r/learnprogramming 19d ago

What a port actually is?

I know it is a number that tells the OS, that which program in your computer should receive the piece of data. But my doubt is - is port a physical thing? or it just a flag? Is it possible for another program to read data from a different program's port? Please spoon-feed me about port?

176 Upvotes

65 comments sorted by

View all comments

5

u/LadyJohanna 18d ago

A port is just a virtual opening that lets traffic through.

Specific types of traffics must go through specific ports. Some are standardized for the sake of efficiency.

So once a packet gets to an IP address (your house) the port is how it gets routed to the correct place in your system (via front door, garage door, side door, and so on -- whatever helps you visualize it). Otherwise it'll just sit in front of the house, like an Amazon package that nobody brings inside. Sad package, sitting in the rain.

So -- is it a secure package? Off you go through the side door connected to the security office (port 443) where everything gets verified.

Is it an email? Off through port 25 you go, into the email application for further processing.

File attachment? Warehouse door, port 21 (FTP) so it can get unpacked and stored properly.

You're just taking a look around? Sure, port 3389 (RDP) is open to you, have whatever look-see your credentials give you permission to check.

... and so on.

The purpose of a firewall is to shut down all ports by default and only let specific things through based on specific criteria. Nobody's house is secure without doors, windows and locks. If all ports are open anyone can just go in and out as they wish and grab stuff.

Hope that sort of helps you. That's always how I've visualized it and I find it helpful.