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,424 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 83

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 83

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 83

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 75

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
 
LVL 83

Expert Comment

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

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 75

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 35
SQL Select Query help 1 34
Need help with another query 10 34
Database-Scoped Permissions 2 10
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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