Fixed: ‘An existing connection was forcibly closed by remote host’

A remote computer is the one which has no physical presence; it can be accessed only through some sort of a computer network. The Remote Host is the computer hosting the network which hosts the remote computer and the remote client is the user of the remote client on the network. This feature has revolutionized a lot of processes and has a great scope in the future as well.

An Existing Connection was forcibly closed by the local host error

However, quite recently, a lot of reports have been coming in of an “an existing connection was forcibly closed by the remote host” error while trying to connect to the remote host. This error is triggered with a socket connection between a client and a server. In this article, we will provide some viable solutions to completely rectify this error and also inform you of the reasons that trigger this 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 address
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.3031

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

    HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.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 “View Code” from the list.
  3. There should be an “Application_Start” method, add the following line of code to that method
    if (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 constructor
    public DBEntities() 
            : base("name=DBEntities") 
        { 
            this.Configuration.ProxyCreationEnabled = false; // ADD THIS LINE !
        }
  3. Check to see if the issue persists after adding this line of code.

Solution 5: Updating .NET Framework

The latest version of the “.NET” Framework is required in order for everything to function smoothly. Therefore, in this step, we will be downloading the latest version from the site and installing it. For that:

  1. Navigate to this link to download the setup.
  2. Execute the “.exe” file in order to start the installation process.
    Running the executable downloaded from Microsoft
  3. Follow the onscreen instructions to install the application on your computer.
  4. Check to see if the issue persists after completing the installation.
ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.