Home Fix Fix: An existing connection was forcibly closed by the remote host

Fix: An existing connection was forcibly closed by the remote host

The An existing connection was forcibly closed by the remote host error occurs when trying to establish a socket connection between the client and server. This error is usually triggered if the remote side closes the connection.

The error can occur if you are sending malformed data to the third-party application or if it senses the data to be malformed. In other cases, it can be an unstable link between the client and server, a glitch in the third-party app or system resource issues.

Here is how to fix the existing connection was forcibly closed by the remote host socket exception error in your computer.

How do I fix an existing connection was forcibly closed by the remote host error?

1. Enable Strong Cryptography

an existing connection was forcibly closed by the remote host

  1. Press Windows Key + R to open Run.
  2. Type regedit and hit enter. This will open the Registry Editor.
  3. In Registry Editor, navigate to the following location:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.3031
  4. Locate the ScsUseStrongCrypto
  5. If the ScsUseStrongCrypto value is not present, navigate to the following path instead:
    HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319
  6. Double-click on the SchUseStrongCrypto
  7. In the Value data field enter 1 and click OK.
  8. Close Registry Editor and try to establish the connection again and check if the issue is resolved.

The aforementioned error will occur if Cryptography is disabled in Registry Editor for a machine which will also disable the usage of TLS 1.2. Try enabling the property in Registry Editor to see if that fixes the issue.

If you are unable to enable strong cryptography through Registry Editor, follow the steps below to do it via PowerShell.

Use PowerShell to enable strong cryptography

  1. Open PowerShell.
  2. Enter the following command one by one and hit enter to execute:
    Set-ItemProperty -Path ‘HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319’ -Name ‘SchUseStrongCrypto’ -Value ‘1’ -Type DWordSet-ItemProperty -Path
    ‘HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319’ -Name ‘SchUseStrongCrypto’ -Value ‘1’ -Type DWord
  3. Once executed, reboot your computer and check for any improvements.

2. Update .NET framework

an existing connection was forcibly closed by the remote host

  1. Go to Microsoft Download centre.
  2. Download Microsoft .NET framework.
  3. Run the downloaded file to execute it.
  4. Follow-on screen instructions to install the latest of .NET framework.
  5. Once installed, reboot the computer and check if the error is resolved.

3. Force use TLS 1.2

  1. Open the global.asax file. You can find it by navigating to the root of the project.
  2. Select View Code from the list.
  3. Scroll down to Application_Start method.
  4. Add the following line of code under the method:
    if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)

            {

                ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;

            }

  1. Save the changes and run the code to see if the error is resolved.

Adding the above-mentioned lines can help you resolve the issue if the application that is triggering the error is configured to use TLS 1.1 by forcing TLS 1.2 usage.


4.  Edit the .Context.cs file

  1. Locate the .context.cs file. You can find it under .edmx > context.tt file.
  2. Open the .context.cs file.
  3. Add the following line to your construction.
    public DBEntities()

        : base(“name=DBEntities”)

    {

        this.Configuration.ProxyCreationEnabled = false; //add the following line to your code

    }

  1. Save the changes and check if the error is resolved.

Adding the above line of codes to your constructor will help you resolve the issue if you are using Entity Framework.


5. Diagnose the error with Wireshark

an existing connection was forcibly closed by the remote hostThe troubleshooting steps in the article above are user-specific and if that did not help you fix the problem, try to diagnose the issue by yourself.

The existing connection was forcibly closed by the remote host usually occurs when the remote side closes the connection. If you are working with a third-party application, the error can occur due to one of the following reasons.

  • The remote site is detecting malformed data being sent from your connection and closing the connection.
  • The application is experiencing a service outage.
  • Your request may have triggered a glitch in the third-party app causing it to malfunction.
  • You or the remote site may have exhausted its system resources.

To diagnose the issue, fire up Wireshark. This will help you to analyze the behaviour and narrow down the problem.

You can filter the Wireshark dump by IP address and port number. Start by looking at the end of the stream and follow the trail back to see where and what went wrong.

Knowing what is causing the ‘an existing connection was forcibly closed by the remote host’ error is the first step towards fixing the problem. While we have listed some common solutions that have helped other users fix the error, the issue can vary depending on the user.

LEAVE A REPLY

Please enter your comment!
Please enter your name here