I have an application that listens on a port for information from scripts on various client servers. And I need to decide what port I should assign to the application to bind to. I can look at this list and know which ports not to assign, but I'm confused about how developers decide to assign ports to their application. Are they simply random, or are there guidelines to use specific开发者_开发百科 port ranges for different types of applications?
It's basically random. Choose a port from one of the unassigned/private ranges in the IANA list and hope for the best.
You might want to ask the server and network administrators if there is a port they want you to use, as they might need to configure firewalls/routers/etc. (They might even know what ports are in use and be able to give you a definitive answer about a usable port, but this would be atypical.)
If your application can't open the port for listening, then you'll want to make sure somebody will see the error message. It probably means something else is using the port, so you'll need to resolve the conflict. Similarly, if the clients connect to something at that port and get "weird" output back, make sure that error gets reported in a suitable fashion.
And don't hard-code this port number into any of your applications. Make it configurable via command-line option, config file, environment variable, etc. If you make it hard to change, then you will certainly need to change it.
Your main goal is to get one that isn't commonly used, beyond that pick something that you like.
I sometimes get creative and find a way to calculate a meaningful yet obscure reference to the app. Often I will convert the characters in the name of the app to ascii values, add them up and depending on the value possible multiply by another number to get it into the 1024-65000 range.
精彩评论