Reputation: 304
I'm writing server code with C# and I'm thinking that is it good (or even possible) to serve all clients (say millions in my dreams) with one port or it is better to use for example 1000 ports and use less-used port or a random port for each client! what is the performance impact?
Upvotes: 0
Views: 713
Reputation: 9650
Using multiple ports for different clients in generally the wrong approach.
A port is a limited resource so you need to manage them as such.
There is a maximum of 65,535 of which you should only use ports between 1024 & 49,151.
See https://en.wikipedia.org/wiki/Registered_port
They are also not without overhead from an administrative perspective. Think about firewalls, proxies, load balancers, routing etc. If you have an app that uses multiple ports then it requires configuring all of these for these type of apps, (which also increases the security risk) and risks a clash with another app which happens to use the same port on a specific server.
Also, the port is not a performance limitation at all.
If you hit a performance bottleneck then you need to change your code or use load balancer to distribute the load to multiple instances of your application either on the same host or on multiple host.
If you think about it, all the web-servers in the world use port 80 (externally at least). So if Google search, Facebook, Instagram etc. can handle such loads on a single port then I'm sure your simple app can also manage this!
You can use different ports for different purposes including different protocols, and this is accepted practice.
Upvotes: 0