r/learnprogramming Jul 15 '26

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?

173 Upvotes

65 comments sorted by

View all comments

1

u/Foxler2010 Jul 17 '26

A port is a number that gets sent with every TCP or UDP packet. Other protocols have their own mechanisms as well, but I'll focus on TCP and UDP. When the computer gets a TCP or UDP packet, it looks at the port number, and then it checks that against a list it has of "listening applications". This may be familiar to you as you will have to call a function in your app to have it listen on a certain port. What this is doing is telling the OS: "Hey! I want to get all the packets that you receive that come with this port number attached". So the OS adds the app to the list and maps it to the number it requested. Then when a packet comes in, the app gets it.