Solved

Why does our Windows service app's TCP connection keep failing with an "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." error?

Posted on 2015-01-05
4
363 Views
Last Modified: 2015-01-09
Hi:

I have a WCF based windows service that reads from an external device (scale) via a TCP connection.
A connection is made to the device when the service initially starts up.
This is the code we use to initiate the connection to the device:
scaleData.Client = New TcpClient(ipAddress, (portNumber))
scaleData.Stream = scaleData.Client.GetStream()

Open in new window

The service is hosted as a Windows service (as opposed to through IIS).

We have two applications that pull data from the WCF service,
an asp.net application and a winforms application.

The request returns data from the stream to the caller/client.
 Try
                    Dim bytes As Int32 = scaleData.Stream.Read(data, 0, data.Length)
                    responseData = Text.Encoding.ASCII.GetString(data, 0, bytes)c
 Catch ex As IOException
                    exceptionMsg = String.Format("IOEXCEPTION!!! :: {0}", ex.Message)
End Try

Open in new window


Both the (winforms) test application, web application and windows service all reside on the same Windows 7 machine.

For the most part, this configuration works fine. But occasionally it fails.(we can't quite determine the pattern to it or specifics to it's occurrence) with the following message:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

Open in new window

Upon failure, we now added retry logic to try to reestablish the TCP connection and read from the stream,
but that doesn't seem to fix the issue. I've also got logging enabled which captures the exceptions to a log file. All the log shows is the above mentioned message.

Does anyone have any suggestions as to what is going on here?
Is this a Firewall/Port/virus scanner issue?

How do we best proceed?

Thanks,
JohnB
0
Comment
Question by:jxbma
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Dan McFadden
Comment Utility
Since you're using Win7 to host the service (as opposed to a server OS), I am curious to how many tcp connections you are opening to the endpoint.  Win7 has a connection limit of 20.  If you were to try to establish a 21st connection, the service host would deny the connection.

As a suggestion, I would try to track the number of connections to the service endpoint to see if you occasionally exceed 20.  A quick way to watch active connections (in and out) is to download TCPView from Sysinternals.  It will display open udp/tcp connection details including origin, destination and a few other interesting tid-bits of info.

Another thought, are you establishing the connection via IP address to port 80/tcp (based on reference to IIS), if you have a proxy on your network that manages connections, it could also be a source of connection problems.

Dan
0
 
LVL 26

Accepted Solution

by:
Dan McFadden earned 250 total points
Comment Utility
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
Comment Utility
Well, the error indicates that it is the remote device that is dropping the connection.  Perhaps it is disconnecting after a period of inactivity on the connection.

You starting point should probably be the supplier/documentation for the device you're connecting to.
0
 
LVL 1

Author Closing Comment

by:jxbma
Comment Utility
Ultimately, since we are trying to read to the device on demand, I reestablish the TCP connection on every request.
This seems to work for now (until I see another better solution).

Thanks for the responses,
JB
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now