Are they only used to specify "which application should use the data? "
yeah I think so. Or a process rather than an application. A process is an instance of an application in memory.
And the port is one field of many fields within a packet(to use a general and useful definition of packet).
When a process is said to listen on a particular port X, it means it is reading or being given packets that have port set to X
Say you have two computers A and B
B has a bunch of different pieces of software, different processes running
each process on A and each process on B associates with ports.. And so when packets go to A or B, the processes know which packets are for them.
In fact it's not just for each process.. It's for a communication channel.. So a process on A wants to exchange data with a process on B. So A picks a port for that communication, and B picks a port for that communication.. And A and B send packets to each other with the source and destination ports set correctly.
If A and B want to communicate on another subject or a related subject, or the user on A or user on B want to.. Then they can use new ports. And so the programs on A and B or users on A and B , can see the two communication channels separately by looking at all packets where the source or destination port is X.
As to your questions
Where do they reside?
Say you have Computer A and Computer B, then Computer A is said to have a port open, and computer B is said to have a port open. It's like an ID, the program would "open a port", i.e. it'd request from the OS that a "port" be set aside for its use.
Can I change them?
There are two cases of a port opening..i.e. a port coming into existence. That ID being created for use.
One is when a program initiates a connection, and the other is when a program is listening for a connection. The listening happens first.
When it comes to making a program listen for a connection, you can often tell the program which port to listen on. Usually the program will give you the option.
You can try this yourself by setting up what is called a "server".. There are different definitions of server.. One is a rather powerful computer, another is a central computer that might be accessed by many others, or even be used to access many others. And another definition - the main definition - is a piece of software that listens for others to connect (often potentially many others to connect), so you see other definitions of server spring from that one.
You can create a server.. set one up
Tinyweb is a little web server
Instructions are on this website
https://www.ritlabs.com/en/products/tinyweb/install.php
so create c:\blah\index.html it can even be blank.
this makes it listen on port 8000
Run TinyWeb on port 8000:
c:\tinyweb\tiny.exe c:\blah 8000
You can then do http://127.0.0.1:8000 So accessing your computer's local (virtual I guess) network interface 127.0.0.1 The so-called loopback interface
And specifying port 8000
If you set up tinyweb on a different port, then you would http to a different port, changing the number after the colon
and view your webpage
Typically people have web servers listen on port 80.. if they're public.. And sometimes port 8080 if they're internal to their own LAN and not public.
Load of programs listen on a port. VNC for example, that is a program that lets you view your computer remotely. It has settings where you specify a port
There is a program called nc that can just listen on a port and output whatever is sent to it. You can run that with cygwin if you install cygwin.
So, The program itself can change which port it listens on. and you can tell the program..
That's for servers, programs that listen on a port
Programs that initiate a connection with the server, are called client programs. You always have a client and a server.. One program that listens (normally called the client) and one program that listens (normally called the server).
Client and server can mean the client requests and the server replies.. and then if the one that replies is the client then you can get some funny terminology from some software about a server making a reverse connection.. to a client. But normally the one that initiates is called a client and the one that listens is called the server, and normally it's also the client that makes the requests and the server making the replies. e.g. your web browser is a client and it requests webpages from the web server at some remote IP.
Clients , i.e. programs initiating connections, they get a random port number above 1024
If you open cmd.exe and run netstat -aon
then you see a list of connections including ports. It shows local port and remote port for any connection.
So look for the port numbers < 1024. Here we have a server listening on port 1234 on my computer (0.0.0.0 means any IP can connect.. though that could be stopped by a firewall, or NAPT router).
And the other line look in the foreign address you see port 80.. that's my computer connecting to a web server
The ports > 1023 are random ports
The first 1024 ports ports 0-1023 are generally the ones that servers use. though sometimes people choose higher ports for servers, as a layer of security, to hide a little bit better to make detection a little bit harder from a hacker. Or out of a convention like 8080 for an internal web server on a LAN.
C:\>netstat -aon
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:1234 0.0.0.0:0 LISTENING 3656
...
TCP 10.0.0.92:50735 147.148.112.39:80 CLOSE_WAIT 5340
How does the ISP blocks a Port(I have read in some articles where ISPs block certain ports numbers of a tenant and so tenant is not able to host a website)
If port number is blocked inside a Router, how is it done?
NAT Routers do it. Though they can run a firewall too and that can do it too
NAT routers are doing a form of NAT with many names including NAPT or NAT/PAT. To unblock it on a NAT router, you'd do "port forwarding". which means that when a request is made and comes across from outside typically the internet, to connect to your server, the router passes it onto one of your computers, the one you tell it to, that runs the server.
The software they'd use is called a firewall. You run one too in Windows XP and Windows 7. There are also dedicated hardware firewalls (they also run firewall software)
Who has reserved certain Port Numbers for Web, SMTP, FTP, etc.
They are conventions
IANA I suppose. And they have a list of them all.
https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt
TCP-IP Ports and how they work – DavidPostill – 2015-06-28T12:34:53.797
Port (computer networking) – DavidPostill – 2015-06-28T12:35:52.593
How can you know the definition but not know what they are? – Scott Hunter – 2015-06-28T16:31:23.327
@ScottHunter well, you could memorize the definition and not understand what you have memorized! like E=M C^2 – barlop – 2015-06-30T13:07:03.160