Link to home
Start Free TrialLog in
Avatar of MrB8r
MrB8rFlag for Australia

asked on

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
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Maybe it's the remote host that is crashing?  Have you or can you check them?
Avatar of MrB8r

ASKER

What does it mean by the "remote host" here?
"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?
Avatar of MrB8r

ASKER

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.
If it's the SQL Server, you have to re-make the connection to get it going again.
Avatar of MrB8r

ASKER

Can I automtically restart the C# application when it crashed?
If so, how to do it?
Can this application be restarted automatically every time it fails?
Why not just add in proper exception handing?
Avatar of MrB8r

ASKER

They are within try catch blocks as I mentioned above, but still crashing. Why is that and what else can I do?
Do any of your 'catch' blocks re-open the connection to the SQL Server?
What exception type are you catching in your catch blocks?
Avatar of MrB8r

ASKER

Do any of your 'catch' blocks re-open the connection to the SQL Server?
No
Avatar of MrB8r

ASKER

What exception type are you catching in your catch blocks?
I had ApplicationException and Exception types.
Also added DataException after your comment
Avatar of MrB8r

ASKER

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

Avatar of MrB8r

ASKER

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)
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MrB8r

ASKER

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).