Reputation: 1464
In an client application I connect many times to the same server to setup a TCP socket.
TcpClient client = new TCPClient();
...
await client.ConnectAsync(host, outport);
It seems, that my dynamic ports are exhausted, because I get "Only one usage of each socket address (protocol/network address/port) is normally permitted
".
This message is a little misleading, but I observe, that it occures always after using the same number of connections.
So I tried to run several servers on different ports. However, this doesn't make a difference - the first error happens after the same number of ports... I'm surprised now:
Is the reserved port range on Windows 10 to be considered
I thought, that 2) is the answer, but based on my observation I'm not sure anymore.
Upvotes: 1
Views: 598
Reputation: 596582
There is only 1 pool of ephimeral ports that all sockets share.
See the following MSDN articles for configuring the available range of ephimeral ports:
The default dynamic port range for TCP/IP has changed since Windows Vista and in Windows Server 2008
Troubleshoot port exhaustion issues
UPDATE: Well, more precisely, there is a limited number of ports per local IP, at least. If you have multiple IP adapters installed (ie, your PC is connected to multiple networks), you can bind multiple sockets to the same Local Port as long as they are bound to different Local IPs. A socket connection is uniquely identified by the tuple [Protocol, Local IP, Local Port, Remote IP, Remote Port]
. So, if you are making a bunch of connections to the same Remote IP/Port using the same protocol (in this case, TCP), then the Local IP/Port pairs must be unique. So, if all of the outgoing sockets are bound to the same Local IP, that limits the available Local Ports you can use. But if you use multiple sockets on Local IPs, that will widen the available Local Ports. But that will only work for your use-case if those multiple networks have routes to the server you are interested in.
Upvotes: 2