Главная » Error » How to Fix ‘An Existing Connection was Forcibly Closed by the Remote Host’ Error?

How to Fix ‘An Existing Connection was Forcibly Closed by the Remote Host’ Error?

What Causes the ‘An existing connection was forcibly closed by the remote host’ Error in Windows?

After receiving numerous reports from multiple users, we decided to investigate the issue and devised a set of solutions to fix it. Also, we looked into the reasons due to which it is triggered and listed them below.

  • TLS 1.1/1.0 Usage: If the application is running on TLS 1.1 or TLS 1.0, it might trigger this error due to them being depreciated. TLS 1.2 is the way to go when selecting the protocol which the application uses.
  • Cryptography Disabled: If Cryptography has been disabled for your machine it will prevent the usage of TLS 1.2 and will fall back on TLS 1.0 which might trigger the error.
  • Socket Implementation: In some cases, a particular type of socket implementation triggers the error. There is a bug with some implementations by “.NET” application and it might cause this error.
  • Missing Code: For some people who were using the Entity Framework, it was observed that a certain line of code was missing due to which the error was being triggered.
  • Outdated “.NET” Framework: In certain cases, if the “.NET” Framework has been disabled, this error might be triggered. Certain tasks require the “.NET” framework to be updated to the latest version in order for them to work properly.

Now that you have a basic understanding of the nature of the problem, we will move on towards the solutions. Make sure to implement these in the specific order in which they are presented to avoid conflicts.

Solution 1: Enabling Cryptography

If Cryptography has been disabled for your machine the usage of TLS 1.2 is prohibited. Therefore, in this step, we will be enabling Cryptography. For that:

  1. Press “Windows” + “R” to open the Run prompt.
  2. Type in “regedit” and press “Enter“.
    Typing in “Regedit” and pressing “Enter”
  3. Navigate to the following addressHKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFrameworkv4.0.3031

    Navigate to this address if there is no “SchUseStrongCrypto” value in the right pane.

    HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoft.NETFrameworkv4.0.30319

  4. In the right pane, double click on the “SchUseStrongCrypto” option and enter “1” as Value data.
    Double clicking on the “SchUseStrongCrypto” value in the right pane
  5. Click on “OK” to save your changes and check to see if the issue persists.

Solution 2: Forcing TLS 1.2 Usage

If the application has been configured to use TLS 1.1 or TLS 1.0 instead of the TLS 1.2, it might trigger this error. Therefore, in this step, we will be configuring our computer to use TLS 1.2. For that:

  1. Navigate to the root of the site and right-click on the “global.asax” file.
  2. Select “ViewCode” from the list.
  3. There should be an “Application_Start” method, add the following line of code to that methodif(ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12)==false)
                {
                    ServicePointManager.SecurityProtocol=ServicePointManager.SecurityProtocol|SecurityProtocolType.Tls12;
                }
    Adding the lines to the code
  4. Save your changes and check to see if the issue persists.

Solution 3: Changing Socket Implementation

If a certain socket implementation has a bug or glitch in it, it might prevent certain elements of the application from functioning properly due to which this error might be triggered. Therefore, in this step, we will be configuring it to use a different implementation. For that:

  1. Make sure you have a “StateObject” class with “public byte[] buffer = new byte[1024], public Socket socket;“.
  2. Call the “Receive(Socket s)” function and call the following code in “void ReceiveCallback(IAsyncResult ar)”SocketError errorCode;int nBytesRec = socket.EndReceive(ar,out errorCode);if(errorCode !=SocketError.Success){
    nBytesRec =0;}
  3. Check to see if the issue persists after implementing this code.

Solution 4: Adding Command Lines (Only for Entity Framework)

If you are using the Entity Framework, it is possible that a certain line of code might be missing. Therefore, in this step, we will be adding that line of code in order to fix this issue. For that:

  1. Open your “.edmx” file and open the “.context.tt” file below it.
  2. Open the “.context.cs” file and add the following line of code to your constructorpublicDBEntities():base(“name=DBEntities”){this.Configuration.ProxyCreationEnabled=false;// ADD THIS LINE !}
  3. Check to see if the issue persists after adding this line of code.

Reconnecting to the server once loss of connection is detected

As the socket object is no longer useful after the connection is lost, it is necessary to create a new socket object (this can be assigned to the initial socket variable).

Once you have recreated the client socket, you can then try to reconnect to the server using the connect() method. The problem with this is that the issue that caused the loss of connection may still be affecting the system, this means the connect() method could result in an exception such as a “ConnectionRefusedError” (caused by the server not actively listening for connections).

A simple solution to this issue is to place the connect() method within a while loop and surround it with a try-except statement. If the connection is successful, then the application will continue with the rest of the script, otherwise it will wait a few seconds and attempt to reconnect again.

print( “connection lost… reconnecting” )
connected = False# recreate socket
clientSocket = socket.socket()

whilenot connected:
    # attempt to reconnect, otherwise sleep for 2 seconds
    try:
        clientSocket.connect( ( host, port ) )
        connected = True
        print( “re-connection successful” )
    except socket.error:
        sleep( 2 )

# continue normal operations

The above script will keep the application within the while loop until it has successfully reconnected to the server socket.

Источники

  • https://appuals.com/how-to-fix-an-existing-connection-was-forcibly-closed-by-the-remote-host-error/
  • https://instructobit.com/tutorial/101/Reconnect-a-Python-socket-after-it-has-lost-its-connection
[свернуть]
Решите Вашу проблему!


×
Adblock
detector