Solved

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

Posted on 2013-12-14
16
5,175 Views
Last Modified: 2014-01-05
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
0
Comment
Question by:MrB8r
  • 9
  • 4
  • 3
16 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39719410
Maybe it's the remote host that is crashing?  Have you or can you check them?
0
 

Author Comment

by:MrB8r
ID: 39719441
What does it mean by the "remote host" here?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39719449
"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
 

Author Comment

by:MrB8r
ID: 39719467
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39719473
If it's the SQL Server, you have to re-make the connection to get it going again.
0
 

Author Comment

by:MrB8r
ID: 39719479
Can I automtically restart the C# application when it crashed?
If so, how to do it?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39719486
Can this application be restarted automatically every time it fails?
Why not just add in proper exception handing?
0
 

Author Comment

by:MrB8r
ID: 39719490
They are within try catch blocks as I mentioned above, but still crashing. Why is that and what else can I do?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39719493
Do any of your 'catch' blocks re-open the connection to the SQL Server?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39719526
What exception type are you catching in your catch blocks?
0
 

Author Comment

by:MrB8r
ID: 39719590
Do any of your 'catch' blocks re-open the connection to the SQL Server?
No
0
 

Author Comment

by:MrB8r
ID: 39719594
What exception type are you catching in your catch blocks?
I had ApplicationException and Exception types.
Also added DataException after your comment
0
 

Author Comment

by:MrB8r
ID: 39721225
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
 

Author Comment

by:MrB8r
ID: 39723215
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
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39724194
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
 

Author Comment

by:MrB8r
ID: 39758384
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now