Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
5,874 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 84

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 84

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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 84

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 84

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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview

971 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