Net error an existing connection was forcibly closed by the remote host

I am working with a commercial application which is throwing a SocketException with the message, An existing connection was forcibly closed by the remote host This happens with a socket connection

For anyone getting this exception while reading data from the stream, this may help. I was getting this exception when reading the HttpResponseMessage in a loop like this:

using (var remoteStream = await response.Content.ReadAsStreamAsync())
using (var content = File.Create(DownloadPath))
{
    var buffer = new byte[1024];
    int read;

    while ((read = await remoteStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
    {
        await content.WriteAsync(buffer, 0, read);
        await content.FlushAsync();
    }
}

After some time I found out the culprit was the buffer size, which was too small and didn’t play well with my weak Azure instance. What helped was to change the code to:

using (Stream remoteStream = await response.Content.ReadAsStreamAsync())
using (FileStream content = File.Create(DownloadPath))
{
    await remoteStream.CopyToAsync(content);
}

CopyTo() method has a default buffer size of 81920. The bigger buffer sped up the process and the errors stopped immediately, most likely because the overall download speeds increased. But why would download speed matter in preventing this error?

It is possible that you get disconnected from the server because the download speeds drop below minimum threshold the server is configured to allow. For example, in case the application you are downloading the file from is hosted on IIS, it can be a problem with http.sys configuration:

«Http.sys is the http protocol stack that IIS uses to perform http communication with clients. It has a timer called MinBytesPerSecond that is responsible for killing a connection if its transfer rate drops below some kb/sec threshold. By default, that threshold is set to 240 kb/sec.»

The issue is described in this old blogpost from TFS development team and concerns IIS specifically, but may point you in a right direction. It also mentions an old bug related to this http.sys attribute: link

In case you are using Azure app services and increasing the buffer size does not eliminate the problem, try to scale up your machine as well. You will be allocated more resources including connection bandwidth.

  • MiniTool

  • MiniTool News Center

  • An Existing Connection Was Forcibly Closed by the Remote Host

By Sonya | Follow |
Last Updated November 28, 2022

google search

What will you do if you get the “An existing connection was forcibly closed by the remote host” error? If you have no idea to deal with it, then this post from MiniTool is what you need. You can find several useful methods in this post.

It is very convenient to manage remote computers using a sort of computer network. But sometimes, you will get an error message stating that “An existing connection was forcibly closed by the remote host” when you try to connect the remote host. Then why does this error occur and how to fix it?

an existing connection was forcibly closed by the remote host

Causes of an Existing Connection Was Forcibly Closed by the Remote Host in Windows

There are several causes of an existing connection was forcibly closed by the remote host, which have been listed below:

  • TLS 1.1/1.0 Usage: If applications are running on TLS 1.1 or TLS 1.0, this error may be triggered because they have been depreciated. TLS 1.2 is the method when selecting the protocol used by the application.
  • Cryptography Disabled: If your computer has cryptography disabled, it will prevent the use of TLS 1.2 and will fall back to TLS 1.0, which may cause the error.
  • Socket Implementation: In some cases, a specific type of socket implementation can cause the error.
  • Missing Code: If you are using Entity Framework, some lines of code missing will trigger this error.
  • Outdate “.NET” Framework: Sometimes, this error may be triggered if the “.NET” framework has been disabled. Some tasks require the “.NET” framework to be updated to the latest version for them to work normally.

How to Fix an Existing Connection Was Forcibly Closed by the Remote Host?

After knowing some causes of the “an existing connection was forcibly closed” error, then this part will offer you some methods to fix the “connection forcibly closed by remote host” error.

Method 1: Enable Cryptography

You can enable Cryptography in Registry Editor to solve the issue. Here is a quick guide:

Tip: You’d better back up your Registry keys before you editing Registry Editor.

Step 1: Press the Win + R keys at the same time to open the Run box.

Step 2: Type regedit in the box and then click OK to open Registry Editor.

Step 3: Go to the following path:

HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoft.NETFrameworkv4.0.30319.

Step 4: With the v4.0.30319 key selected in the left panel, double-click SchUseStrongCryto in the right panel and change the Value data to 1. Click OK to save changes.

Step 5: Restart your computer and then check if the problem that an existing connection was forcibly closed by the remote host is solved.

Method 2: Change Socket Implementation

An error or failure in a socket implementation may prevent some elements of the application from functioning properly, which will cause the “An existing connection was forcibly closed by the remote host” error. Thus, you can change socket implementation to solve the problem.

Step 1: Make sure you have a StateObject class with public byte[] buffer = new byte[1024], public Socket socket.

Step 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;
}

Step 3: After implementing this code, restart your computer to check if the error is fixed.

Method 3: Add Command Lines (Only for Entity Framework)

You may lose some certain lines of code if you are using the Entity Framework, so you can try to add command lines to solve the problem that “An existing connection was forcibly closed by the remote host”.

Step 1: Open your .edmx file and open the .context.tt file below it.

Step 2: Open the .context.cs file and add the following command lines to your constructor:

public DBEntities()
: base(«name=DBEntities»)
{
this.Configuration.ProxyCreationEnabled = false; // ADD THIS LINE !
}

Step 3: After you have added the command lines, reboot your computer to check if

Method 4: Update .NET Framework

If you don’t get the latest version of the .NET Framework, then you may receive the error message stating that an existing connection was forcibly closed by the remote host. Therefore, you need to update the .NET Framework to deal with the problem:

Step 1: Go to this link to download the setup.

Step 2: Run the .exe file to start the installation process, and then follow the instructions showing on the screen to finish installing the application on your computer

Step 3: Restart your computer to see whether the error is fixed.

About The Author

Sonya

Position: Columnist

Author Sonya has been engaged in editing for a long time and likes to share useful methods to get rid of the common problems of Windows computers, such as Windows Update error. And she aims to help more people to protect their data. What’s more, she offers some useful ways to convert audio and video file formats. By the way, she likes to travel, watch movies and listen to music.

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_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 “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.

Photo of Kevin Arrows

Kevin Arrows

Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.

Microsoft.AspNetCore.Connections.ConnectionResetException:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Pipelines.PipeCompletion.ThrowLatchedException (System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at System.IO.Pipelines.Pipe.GetReadResult (System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at System.IO.Pipelines.Pipe.GetReadAsyncResult (System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody+d__4.MoveNext (Microsoft.AspNetCore.Server.Kestrel.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.IO.Pipelines.PipeCompletion.ThrowLatchedException (System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at System.IO.Pipelines.Pipe.GetReadResult (System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at System.IO.Pipelines.Pipe.GetReadAsyncResult (System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody+d__21.MoveNext (Microsoft.AspNetCore.Server.Kestrel.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream+d__22.MoveNext (Microsoft.AspNetCore.Server.Kestrel.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.WebUtilities.BufferedReadStream+d__37.MoveNext (Microsoft.AspNetCore.WebUtilities, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream+d__36.MoveNext (Microsoft.AspNetCore.WebUtilities, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream+d__35.MoveNext (Microsoft.AspNetCore.WebUtilities, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions+d__3.MoveNext (Microsoft.AspNetCore.WebUtilities, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Http.Features.FormFeature+d__18.MoveNext (Microsoft.AspNetCore.Http, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProviderFactory+d__1.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider+d__3.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider+d__2.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider+<>c__DisplayClass0_0+<g__Bind|0>d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+d__13.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__23.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__18.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__16.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Builder.RouterMiddleware+d__4.MoveNext (Microsoft.AspNetCore.Routing, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware+d__6.MoveNext (Microsoft.AspNetCore.Diagnostics, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
Inner exception System.Net.Sockets.SocketException handled at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw:
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitable.GetResult (Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection+d__25.MoveNext (Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection+d__24.MoveNext (Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets, Version=2.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)

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_MACHINESOFTWAREMicrosoft.NETFrameworkv4.0.3031
  4. Locate the ScsUseStrongCrypto
  5. If the ScsUseStrongCrypto value is not present, navigate to the following path instead:
    HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoft.NETFrameworkv4.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:SOFTWAREWow6432NodeMicrosoft.NetFrameworkv4.0.30319’ -Name ‘SchUseStrongCrypto’ -Value ‘1’ -Type DWordSet-ItemProperty -Path
    ‘HKLM:SOFTWAREMicrosoft.NetFrameworkv4.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.

Have you recently been facing the “An existing connection was forcibly closed by the remote host” error on your system? This problem usually happens when you try connecting with any remote host, and the process fails.

After going through the various complaints and grievances reported by the users, we looked into the issue and carved out the best solutions.

An existing connection was forcibly closed by the remote host

Be patient and read the article.

What are the reasons behind the “An existing connection was forcibly closed by the remote host” error?

The lack of a physical presence characterizes the remote computer. This means that the only way to access this computer is through a computer network. The computer that hosts the network for the particular remote computer is called the Remote Host.

The user that operates the remote computer through the network is called the Remote Client. This is a great feature and revolutionizes computing processing.

However, the host and client connection has recently been facing errors.

But before we begin, let us also take you through the causes that are the potential problem-makers so that you do the fixing better:

  • Disabled cryptography – If you have not noticed that the feature of Cryptography is disabled on the machine you are using, it will hinder the machine from using TLS 1.2, and it will get the application back to TLS 1.0 usage. This auto depreciation triggers the error.
  • The usage of TLS 1.0 / 1.1 – If the application uses TLS 1.0 or TLS 1.1 for operation and storage, there is a high chance that the depreciated TLSs are causing the error. The right type of TLS to be used is TLS 1.2, while selecting the right protocol to be used by the application.
  • Faulty socket implementation – There are some cases where a specific socket implementation becomes the reason behind the lost connection error. The associated .NET application that causes the implementations might have got some bugs and become the troublemaker.
  • Lost code – Some users working with the Entity Framework also faced the error. This was caused by a specific line of code that was lost, causing trouble in the operation of the application.
  • An older .NET Framework – In some cases, the “An existing connection was forcibly closed by the remote host” error was being caused. The reason was the .NET framework being disabled. Some system tasks need the latest framework version installed on the PC to run properly. If the condition is not fulfilled, then they cease to function.

Five Best Methods to Fix the Connection Error

Pick any of these solutions per the issue causing the error.

Besides, here, you will figure out what is the ETD Control Center.

Method 1: Enable the Cryptography

One of the prime reasons you have seen the error message saying, “An existing connection was forcibly closed by the remote host,” is that TLS 1.2 is prohibited from running on the machine.

This hindered the Cryptography from being operational thereby and caused the problem. A logical solution is to enable Cryptography, which will potentially solve the issue.

Below is the series of steps that you need to follow to enable the Cryptographic operation:

1. Press the keys Windows + R together to launch the prompt of the Run Dialogue box.

2. Enter the command regedit. Now hit the button to enter.

regedit

3. Navigate down and then find the address:

KEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFrameworkv4.0.3031

4. Look inside the right pane, and if you don’t find any value like “SchUseStrongCrypto,” then you will have to find this address:

HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoft.NETFrameworkv4.0.30319

5. Again, look through the right window pane and hit right-click over the option of “SchUseStrongCrypto.” Then enter the number 1 as the value of the data being entered.

Enable the Cryptography

6. Lastly, click on the option OK to save the changes you made successfully.

Now go back to the operation you were running previously and see if the error is gone.

Method 2: Force launching the usage of TLS 1.2

It might not have come to your notice, but the application you have been trying to run might have experienced certain changes in its configuration.

Sometimes the user settings of any application are set at TLS 1.0 or TLS 1.1, while the correct option should be TLS 1.2.

This change can become the reason for the error “An existing connection was forcibly closed by the remote host.”

This method will deal with the error by changing such system configurations and setting it to TLS 1.2. Follow the steps given below:

1. Navigate and find the root folder of the particular website. Now hit a right-click over the file with the name global.asax.

2. Select the option of View Code from the given list.

3. Find the option of the method named ‘Application_Start‘ that must be in the next window pane. The line of code that we give you down below needs to be added in the particular method:

if (ServicePointManager .SecurityProtocol.HasFlag (SecurityProtocoType.Tls12) == false)
{
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}

Force launching the usage of TLS 1.2

4. Save these changes that you made.

Go back to running the previous operation and see if the method has removed the error.

Method 3: Change the Socket Implementation

A faulty socket implementation is another probable reason why the error “An existing connection was forcibly closed by the remote host” has been creating trouble with the program you wanted to operate.

Sometimes, any glitch or bug that might have affected the socket implementation related to the program happens.

This hinders the program’s concerning elements from running smoothly, and the error, as mentioned earlier, is the result. In this method, we sought to configure and set up a new implementation, which will be different from the previous one.

Follow the steps we give below:

1. Before starting the process, check properly and ensure that the class StateObject is available to you. The code “public byte[] buffer = new byte[1024], public Socket socket;” should be there too.

2. Make a call over the function named “Receive (Socket s)” and then call in the mentioned code “void ReceiveCallback(IAsyncResult ar)

SocketError errorCode;
Int nBytesRec = socket.EndReceive (ar, out errorCode);
If (errorCode != SocketError.Success)
{
nBytesRec = 0;
}

Once you have successfully implemented this code, go back, and see if the error remains. It would probably be solved by now.

If it is not, move ahead with the next methods that we give you for dealing with the error “An existing connection was forcibly closed by the remote host.”

Method 4: Add the Command Lines (for the users of Entity Framework)

If you have been a user of the Entity Framework, then there might be a possibility that a particular line of code must have been left out.

We aim to deal with this issue in this method, where we will add the missing line of code to fix the final host error issue.

Follow the steps below to do so:

1. Open the file named ‘.edmx.’ on your system. You will find a file named ‘.context.tt‘ below the previous file. Open it.

2. Now open the file named ‘.context.cs‘. In the associate constructor, you need to enter the line of code that we give you below:

Public DBEntities ()
: base(“name=DBEntities”)
{
This.Configuration.ProxyCreationEnabled = false; // ADD THIS LINE !
}

Once the command runs successfully, go back to the application you were trying to open and see if it has gone error-free.

Method 5: Update the .NET Framework

Many elements need to operate nicely for any application to run smoothly. The .NET framework is one such component, and you need to have the latest version of this framework installed on your PC to ensure the proper functioning of the related applications.

Therefore, this method will deal with installing the updated version of the .NET framework from the right website.

Follow the steps to do so:

1. Open Microsoft’s official website to download the setup of the concerned framework. Finish the download and begin to install the setup properly.

2. Find the .exe file in the setup and open it to execute it. This will begin the process of installing the framework program.

Update the .NET Framework

3. Keep following the instructions as they appear on the screen for installing the application properly on the PC that you are using.

Now go back to run the program that you were previously trying to. See if this method successfully removes the error “An existing connection was forcibly closed by the remote host.”

Conclusion

The error “An existing connection was forcibly closed by the remote host” hampers the connection between the server and the client. We hope that the solutions helped rectify the error and eliminate it.

This website uses cookies to ensure you get the best experience on our website

Like this post? Please share to your friends:
  • Need for speed undercover ошибка установки
  • Mysql server error 18456
  • Near show syntax error
  • Mysql rise error
  • Mysql replica skip error