Choosing a Port

A common question is what port should be chosen for publishing a service. I'm assuming that the question is being asked because no one has told you that you must use a certain port for contractual or operational reasons. If you have to use a particular port, then there isn't a lot of choice in the matter. If you aren't forced to use a particular port, then it becomes more of a game.

Several protocols have the concept of sharing a port between multiple listeners. If that's the case and you're willing to share, then the default port number for the protocol is typically a good choice. That's port 80 for HTTP, port 443 for HTTPS, or port 808 for Net.Tcp. With port sharing, you only need to be creative in the selection of the path portion of the listener address.

When you have port sharing but you're not willing to share, you want to do exactly the opposite and avoid the default ports. That minimizes your chances of breaking a whole bunch of applications that use the default port. It only takes one non-sharing application to block everybody else out. The same is true if you have a protocol with no concept of port sharing at all. In either case, you want to pick a port number that is unique enough to not conflict with anyone else on the system but not so unique that it causes you trouble.

A good rule of thumb is that the port number should be between 5000 and 10000. The lower numbered ports tend to have lots of people already claiming that space. The number 5000 is a historical relic from the DCOM days as a recommendation; it doesn't actually have a special significance for web services. The higher numbered ports tend to start straying into the range of randomly generated client ports. This isn't a problem if the service is already running because the random port generator won't pick that port number. However, it is a problem if the client port is picked first and then your service tries to start because the port will be temporarily allocated. The solution is not quite as simple as just avoiding the client ports that Windows uses because proxy servers and other intermediaries might be forced to use an equivalent port but be using a different client port allocation algorithm.

Next time: Custom Namespaces

Comments (3)
  1. I have a hierarchy of types that I’m using in service data contracts. When I change the service implementation,

  2. tashbarg says:

    You’re sure you know something of TCP/UDP ports?

    I suggest some reading of:

    or even Wikipedia

    but don’t write howtos if you don’t know the subject.

Comments are closed.

Skip to main content