I'm sorry if this is a stupid question but these are the kind of decisions I get stuck on.
In your applications, how do you choose a port number that is unlikely to be used by another process?
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (10)
I will randomly pick a port number in the thousands (e.g.
7777
) to run my application on and it is typically free.If I ever have a collision, my application fails to start and tells me the port is in use. This happens when I previously started an application on my go-to port number, so I restart my application with the port number increased by one.
I generally avoid port numbers below 1024 because they're considered "system ports" and require root access.
California Law.
Go here: leginfo.legislature.ca.gov/faces/c...
Search for a keyword, maybe related in some way to your application, maybe just something you're interested in (beer, wine, liquor, etc.)
Find something above 2000 in the search results but usually below 40000 (stay away from 26000 because you don't want to be messing with anyone's Quake server).
Click on that result. Now you've learned something AND you have a port number for your application.
Random high 4-digit (or more) integer to keep it out of the reserved 1024 ports. I try to keep in round, like 867000 and not 8675309
If I'm writing software that will be used on random people's PCs or on computers where there might be a wide variety of software running I will take a deliberate step and search on Google for potential conflicts.
Otherwise, the random port number on an isolated single-purpose system such as a Raspberry Pi or Arduino or even an internal web server is probably good enough.
Actually 867000 is out of the valid range of 0 - 65535 :D
Haha! Whoops. Well, shave some digits off those random numbers.
Well In my case I have a Rails backend that uses by default the port 3000 and the two apps that use this backend are React Apps that by default they use the same port.
What I did is leave the port 3000 to the rails app and then change the port in the package.json of the react apps one with 3001 and the other 3002, with this I can start all the apps together.
The way I organized it for applications and web APIs on an internal network was to use a 4 digit number and have the first two numbers indicate the server and the second two indicate the application/API type. I went with even numbers for applications and odd ones for APIs. So, for example, 8891 might indicate a service on the accounting production server while 8792 might indicate an application on the shipping test server.
If it's above 1024 and isn't already in the list of common ports it's good enough.
This is a good rule of thumb!
:P