TCP application fail - Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host

We are having a real-time remote monitoring application running on a server which connects with various loggers on sites via GSM modems and TCP/IP connection.

This server application requests data from the field loggers continuously (varying from every minute or 5 minute or 15 min intervals) and writes to SQL server database tables.

Recently the application started crashing every now and then giving one of the below 2 error messages in the exception details.

I have disabled the windows firewall on the server computer, but no difference.

Can someone help with a solution to stop this?
Can this application be restarted automatically every time it fails?

1.
Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. : System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)

2.
A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.) : .Net SqlClient Data Provider
MrB8rAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
I had ApplicationException and Exception types.
Also added DataException after your comment
You cannot just assume which exceptions you are going to be catching, you have to look at the documentation to see what the function might throw. SocketException inherits from neither ApplicationException nor DataException. It inherits thus:

Screenshot
This is why your first two catch blocks didn't work, and only the last one did--all exceptions in .NET ultimately inherit from System.Exception, even ApplicationException does.

So, now that we know you have exception handling that is catching the exception (in some fashion), how are you attempting to recover from the error?
0
 
Dave BaldwinFixer of ProblemsCommented:
Maybe it's the remote host that is crashing?  Have you or can you check them?
0
 
MrB8rAuthor Commented:
What does it mean by the "remote host" here?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Dave BaldwinFixer of ProblemsCommented:
"remote host" is the device or computer that you are connecting to.  If you got this message in your web browser when you were searching for something on Google, Google would be the "remote host".  In you situation, it would be the "field loggers" or the TCP/IP connection at their location.

Hmm.  Your second error message says SqlClient.  Is the SQL Server on another computer?
0
 
MrB8rAuthor Commented:
It's a C# application hosted on Windows 2008 R2 server which is giving the error messages.

Yes, SQL server is on another computer.

When these crashes happens, C# application stops working even almost all the functions are within try catch blocks.
0
 
Dave BaldwinFixer of ProblemsCommented:
If it's the SQL Server, you have to re-make the connection to get it going again.
0
 
MrB8rAuthor Commented:
Can I automtically restart the C# application when it crashed?
If so, how to do it?
0
 
käµfm³d 👽Commented:
Can this application be restarted automatically every time it fails?
Why not just add in proper exception handing?
0
 
MrB8rAuthor Commented:
They are within try catch blocks as I mentioned above, but still crashing. Why is that and what else can I do?
0
 
Dave BaldwinFixer of ProblemsCommented:
Do any of your 'catch' blocks re-open the connection to the SQL Server?
0
 
käµfm³d 👽Commented:
What exception type are you catching in your catch blocks?
0
 
MrB8rAuthor Commented:
Do any of your 'catch' blocks re-open the connection to the SQL Server?
No
0
 
MrB8rAuthor Commented:
What exception type are you catching in your catch blocks?
I had ApplicationException and Exception types.
Also added DataException after your comment
0
 
MrB8rAuthor Commented:
Data insert (to the database) function is within a try block and catch DataException.
But it didn't catch the exception there and caught by the general exception.
try
{
try
{
DataInsertToTheDatabase()
}
catch (DataException e)
{
Did not catch it here...
}
}
catch (ApplicationException e)
{
Did not catch it here...
}
catch(Exception e)
{
caught here
}

Open in new window

Exception details...

Exception: A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParserStateObject.WriteSni()
   at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()
   at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

Open in new window

0
 
MrB8rAuthor Commented:
Another exception details of same type...

Exception: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)

InnerException: System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
0
 
MrB8rAuthor Commented:
Thanks kaufmed and others. Yes, I have managed to catch the exception now so that it won't crash. But couldn't recover from the error. For now I am going to accept the above as the answer and try to fix the errors later (errors mentioned above are still there).
0
All Courses

From novice to tech pro — start learning today.