Categories
.net c# ioexception

Unable to read data from the transport connection : An existing connection was forcibly closed by the remote host

155

I have a server app and sometimes, when the client tries to connect, I get the following error:

enter image description here

NOTE: the “couldn’t get stream from client or login failed” is a text that’s added by me in catch statement

and the line at which it stops ( sThread : line 96 ) is :

tcpClient = (TcpClient)client;
clientStream = tcpClient.GetStream();
sr = new StreamReader(clientStream);
sw = new StreamWriter(clientStream);

// line 96:                 
a = sr.ReadLine();

What may be causing this problem? Note that it doesn’t happen all the time

1

  • 1

    ALL THIS MEANS is that you can just upgrade your Framework to something like 4.7.2 and it will probably work without any further issues. It worked for me. It upgrades the security protocols used for transport level communication.

    Jan 29, 2021 at 16:10

97

This error usually means that the target machine is running, but the service that you’re trying to connect to is not available. (Either it stopped, crashed, or is busy with another request.)

In English:
The connection to the machine (remote host/server/PC that the service runs at) was made but since the service was not available on that machine, the machine didn’t know what to do with the request.

If the connection to the machine was not available, you’d see a different error. I forget what it is, but it’s along the lines of “Service Unreachable” or “Unavailable”.

Edit – added

It IS possible that this is being caused by a firewall blocking the port, but given that you say it’s intermittent (“sometimes when the client tries to connect”), that’s very unlikely. I didn’t include that originally because I had ruled it out mentally before replying.

7

  • 2

    the thing is that when i start the server there are some like 50 client connecting to my server . I’ve implemented kind of a wait signal when accepting a client .. something like while (Program.waitToFinishLoginAtClient == true && ajutor < 30) { Thread.Sleep(300); ajutor++; } client = this.tcpListener.AcceptTcpClient(); Program.waitToFinishLoginAtClient = true; ……….. and Program.waitToFinishAtClient gets modified in the thread that contains the client

    – Alex

    Mar 24, 2011 at 14:59


  • 1

    could this “wait” be the problem ?

    – Alex

    Mar 24, 2011 at 15:02

  • 2

    I think the Wait is the problem. I don’t know enough of your code to be sure, but that sure sounds likely. Just curious as to whether you built your own service the “hard way” or if you’re using WCF or even Remoting for this…

    – David

    Mar 24, 2011 at 15:07

  • 1

    Based on the little nit of code here, it looks to me like the “wait” problem could be avoided if it was inside a separate thread for each conneciton. Just in case that guess is right, here’s an example of a multi-threaded TCP Service with multi-threading that may help you: switchonthecode.com/tutorials/…

    – David

    Mar 24, 2011 at 15:10


  • 1

    my existing server is made exactly like the example in the tutorial .. except the wait . The thing is that it was working fine till recently ..

    – Alex

    Mar 24, 2011 at 20:49


97

This error usually means that the target machine is running, but the service that you’re trying to connect to is not available. (Either it stopped, crashed, or is busy with another request.)

In English:
The connection to the machine (remote host/server/PC that the service runs at) was made but since the service was not available on that machine, the machine didn’t know what to do with the request.

If the connection to the machine was not available, you’d see a different error. I forget what it is, but it’s along the lines of “Service Unreachable” or “Unavailable”.

Edit – added

It IS possible that this is being caused by a firewall blocking the port, but given that you say it’s intermittent (“sometimes when the client tries to connect”), that’s very unlikely. I didn’t include that originally because I had ruled it out mentally before replying.

7

  • 2

    the thing is that when i start the server there are some like 50 client connecting to my server . I’ve implemented kind of a wait signal when accepting a client .. something like while (Program.waitToFinishLoginAtClient == true && ajutor < 30) { Thread.Sleep(300); ajutor++; } client = this.tcpListener.AcceptTcpClient(); Program.waitToFinishLoginAtClient = true; ……….. and Program.waitToFinishAtClient gets modified in the thread that contains the client

    – Alex

    Mar 24, 2011 at 14:59


  • 1

    could this “wait” be the problem ?

    – Alex

    Mar 24, 2011 at 15:02

  • 2

    I think the Wait is the problem. I don’t know enough of your code to be sure, but that sure sounds likely. Just curious as to whether you built your own service the “hard way” or if you’re using WCF or even Remoting for this…

    – David

    Mar 24, 2011 at 15:07

  • 1

    Based on the little nit of code here, it looks to me like the “wait” problem could be avoided if it was inside a separate thread for each conneciton. Just in case that guess is right, here’s an example of a multi-threaded TCP Service with multi-threading that may help you: switchonthecode.com/tutorials/…

    – David

    Mar 24, 2011 at 15:10


  • 1

    my existing server is made exactly like the example in the tutorial .. except the wait . The thing is that it was working fine till recently ..

    – Alex

    Mar 24, 2011 at 20:49


39

My specific case scenario was that the Azure app service had the minimum TLS version changed to 1.2

I don’t know if that’s the default from now on, but changing it back to 1.0 made it work.

You can access the setting inside “SSL Settings”.

3

  • 1

    I had this issue for a new powershell Azure function, but changing it to TLS1.0 doesn’t help. Any reason why this i happening?

    Feb 11, 2021 at 12:16

  • 1

    This isn’t a great solution, what does one do if TLS is set to 1.2 on the server and it’s forbidden to change it. (No access, no Admin, 100 other stakeholders, etc…)

    – Reahreic

    Feb 24, 2021 at 12:55

  • 1

    @Reahreic Update your app to use 1.2

    – TylerH

    Aug 2 at 21:04