High performance C # TCP server: the connection could not be completed because the target machine actively refused it

I designed a TCP server according to your advice: High-performance TCP server in C #

It is based on an asynchronous pattern.

I also developed a stress test application to test its performance. My server can receive thousands of connections in parallel from my stress testing application, can analyze data and save it in my database.

When I underline my server, I can get a "System.Net.Sockets.SocketException". No connection can be made because the target machine actively rejected it, β€œan error from my server, so I have to connect to it. This is with 5000 simultaneous connections, I have to try to reconnect due to this problem 10-20% of connections. if I test it with 10K parallel connections, it can be 30-40%. Sometimes it can be - very rarely - more than 50%. It seems that it can not cope with the connection: I am making new connections from my stress test like this as strong as my test computer - about 120 plug Nij / sec.

So what can cause such an exception? How to deal with this? What to do on the server side to avoid this problem? How to configure a TCP connection?

Thanks in advance!

+6
source share
2 answers

You are making connections faster than the software can listen for new connections, or, in other words, you are reaching the connection limits per second of this port. I think you can double the number of connections per second by listening to the second port, the client side, which you should simply restore when you get the exception.

There are also restrictions on the number of compounds for which see Chris O's answer .

+2
source

From time to time, you may not have available ports. You can easily view it with the TcpView utility from SysInternals.

On Windows, when you free a port, it does not immediately go into an accessible state, but instead sits in TIME_WAIT state for a certain interval. Until he leaves this state, the application will not be able to use this port. The time delay, the maximum number of ports and the available port ranges differ from OS, XP and Win7 vs Win2008 Server.

There are two registry entries that can reduce this time interval: HKLM/System/CurrentControlSet/Services/Tcpip/Parameters/TCPTimedWaitDelay

and increase the maximum number of ports that can be opened by the application: HKLM/System/CurrentControlSet/Services/Tcpip/Parameters/MaxUserPort

EDIT: MaxFreeTcbs seems to be the third parameter that could help (I haven't tried this yet) mentioned in this TechNet article , which has more recommendations for tracking odd network issues. NTN.

+6
source

Source: https://habr.com/ru/post/889766/


All Articles