mark_c wrote:I am doing tests to understand how many TCP connections can manage windows simultaneously, and I realized that after a certain number of connections, but I have not yet understood how many, if you try to connect to the internet with a browser network traffic everything is blocked.
Your code is likely wasting available ephemeral ports, making them unavailable for your app, or other apps, to use. Running out of available ports for apps to use is commonly known as "port exhaustion"
Every time a new outbound connection is being created (such as with TClientSocket::Open()), it has to be bound to a local IP/port pair before it can then connect to a remote IP/port pair. If a client does not explicitly request a local IP/port to bind to (TClientSocket does not natively support explicit local bindings, but it is possible by calling Winsock's bind() function directly in the TClientSocket::OnConnecting event), the socket implicitly binds to a local pair on the client's behalf, using an available ephemeral port from the OS. There are a limited number of such ports available.
When a socket is closed, if it is the 1st party to close the connection (which your code implies you are), the connection goes into the TIME_WAIT state (see the WinSock state diagram
). During that time, the OS holds on to the local IP/port pair even though the socket has been closed, preventing that pair from being reused by subsequent connections, unless an app explicitly requests to reuse that pair by enabling the SO_REUSEADDR option via Winsock's setsockopt() function (which your code is not doing) when creating a new socket connection.
You can use the command-line netstat
tool to see if you are leaving a lot of socket connections in the TIME_WAIT state when closing your test sockets.
Also, FYI, since you are using ctNonBlocking mode, TClientSocket::Open() runs asynchronously
, exiting immediately and establishing the connection in the background (I've explained that to you earlier). You are Close()'ing each socket before they have a chance to actually connect to the requested server. But, each socket is still being bound to a local IP/port pair (since you are connecting to IP addresses instead of hostnames, so there is no delay in binding the sockets before Open() exits). You should wait for the TClientSocket::OnConnect or TClientSocket::OnError(eeConnect) event to be fired before moving on to the next IP in your list. Otherwise, use ctBlocking mode instead, so that TClientSocket::Open() does not exit until the connection is fully established/failed.
mark_c wrote:sorry but in the meantime I discovered that the cause of the block is still the error 10055 despite always closing every socket
10055 is WSAENOBUFS:
No buffer space available.
An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.
That is a possible outcome when you run out of available ephemeral ports.