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.
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:
- Press “Windows” + “R” to open the Run prompt.
- Type in “regedit” and press “Enter“.
- 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
- In the right pane, double click on the “SchUseStrongCrypto” option and enter “1” as Value data.
- 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:
- Navigate to the root of the site and right-click on the “global.asax” file.
- Select “View Code” from the list.
- 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; }
- 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:
- Make sure you have a “StateObject” class with “public byte[] buffer = new byte[1024], public Socket socket;“.
- 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; }
- 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:
- Open your “.edmx” file and open the “.context.tt” file below it.
- 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 ! }
- 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:
- Navigate to this link to download the setup.
- Execute the “.exe” file in order to start the installation process.
- Follow the onscreen instructions to install the application on your computer.
- Check to see if the issue persists after completing the installation.