I am still a beginner in Socket Programming, and I have come across several TCP/UDP Protocol based codes in which different portNumbers are used, but they all seem so random, also, I have set up client and server on my Virtual Machine, and every code works fine.
The port numbers used are 8080, 22000, 45434, etc.
I got a meaningful explanation for port 8080 but all others are so random, and I cannot seem to find their purpose on Google.
CodePudding user response:
They are essentially random, with no inherent meaning. The TCP protocol doesn't care which ports you use.
There are a few conventions:
- Valid port numbers are from 1 to 65535.
- Ports 49152-65535 are chosen (randomly) by your operating system when you make an outgoing connection and don't choose a port yourself. Both ends of a connection have a port number, but usually we only care about the server's port number, and the client OS chooses a random one.
- On Linux-like operating systems, only the admin user ("
root
") can bind to ports 1-1023. Windows has no such restriction.
Most kinds of servers have "default" ports that the client will use by default. HTTP is 80, HTTPS is 443, SMTP is 25, Minecraft is 25565, Counter-Strike is 27015. You can usually use a different port but then the client has to be told what the port is. If you want to go to Stack Overflow on port 443 you can type in https://stackoverflow.com/ but if you want port 444 you have to type https://stackoverflow.com:444/ - it doesn't work, of course, because they do use port 443.
Important Internet services like HTTP are registered with the Internet Assigned Numbers Authority. Non-important services like games usually just pick them at random between 1024 and 49151. If you want to run two games on the same server and they both use the same port, you have to change one, but that doesn't happen very often. People tend to want memorable ones, so if you want to run two web servers, then 8080 and 8000 and 81 are popular. 1234 and 12345 are popular choices for toy programs. It doesn't make any difference.
CodePudding user response:
Ports 1025 and above are basically open for use by any user-space programs, but there are conventions here. On POSIX-type systems you'll usually have something like /etc/services
which outlines what the intent is for any given port number.
When assigning your own port to a service you're creating, you can pick as you see fit, but the best practice is to avoid a known service to prevent any confusion.
Ports 1-1024 are often restricted to processes with specific access, where the intention here was to prevent regular users from spinning up things like fake SSH or HTTP servers. While you can pick a lower port number, keep in mind this may inconvenience users looking to use your program.