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
433 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 27

Expert Comment

by:Dan McFadden
ID: 40533092
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 27

Accepted Solution

by:
Dan McFadden earned 250 total points
ID: 40533097
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
ID: 40533164
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
ID: 40540434
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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