An Existing Connection Was Forcibly Closed by the Remote Host

An existing connection was forcibly closed by the remote host

This generally means that the remote side closed the connection (usually by sending a TCP/IP RST packet). If you're working with a third-party application, the likely causes are:

  • You are sending malformed data to the application (which could include sending an HTTPS request to an HTTP server)
  • The network link between the client and server is going down for some reason
  • You have triggered a bug in the third-party application that caused it to crash
  • The third-party application has exhausted system resources

It's likely that the first case is what's happening.

You can fire up Wireshark to see exactly what is happening on the wire to narrow down the problem.

Without more specific information, it's unlikely that anyone here can really help you much.

C# HttpClient An existing connection was forcibly closed by the remote host

I don't see in your code sample where you are setting the value of _baseUrl, but I'm assuming that is being done somewhere. I'm also assuming that since this related to payments, the URL is HTTPS. If the remote host has disabled TLS 1.0 and your connection is coming in as TLS 1.0, it could cause that behavior. I know C# 4.6 has TLS 1.0/1.1/1.2 support enabled by default, but I think C# 4.6 still defaults to only SSL3/TLS 1.0 even though TLS 1.1 and 1.2 are supported. If this is the cause of the issue, you can manually add TLS 1.1 and 1.2 to the enabled values using the following code.

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

An existing connection was forcibly closed by remote host

I think I solved my own issue.
Since I have the exception triggered when sending data and the endpoint is off or not connected, I should check if it is connected or not before sending the data.

However, there is no straight forward way to find if the endpoint is connected.
Since when I send data and the endpoint is off it triggers exception, the exception became my indicator that the endpoint is off and connection is lost. So what I do is that I create the Socket again within the "Catch" statement and try again every period of time until the Socket is connected again.

The reason I create the Socket again every time exception triggered is because the Socket will be unstable and will lead to many connection issues. So better to create it again instead of having instability.

Here is the code I included

               private void MessageCallBack(IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if(size>0)
{
byte[] receivedData = new byte[1500];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receivedMessage = eEncoding.GetString(receivedData);
listBox1.Invoke(new MethodInvoker(delegate {
listBox1.Items.Add("From a friend: " + receivedMessage); }));
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None,
ref epRemote, new AsyncCallback(MessageCallBack), buffer);

}
catch(Exception exp)
{ /* here is the solution, when the Catch is triggered, close the
socket and recreate it to avoid the issues that I been having of
not sending messages and instability. I also added some functions
that would keep sending the status and //being checked on the other
side in order to keep online/offline status.*/

sck.Close();
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
connectEndPoint();
//MessageBox.Show("Exception Small Chat: " + exp.ToString());
}
}


Related Topics



Leave a reply



Submit