C# TcpClient Edge-Case Problem

Status
Not open for further replies.

kilnborn

Ars Scholae Palatinae
700
The problem:

I've got 5 devices (non-computers... industrial devices) acting as TCP Servers. I've written software in C# to use TcpClient to connect to all 5 and then communicate with them.

What happens is that the workers in the plant will unplug a device to charge their cellphone. This breaks the connection to the device. At some point, that station needs to operate, so they unplug their cellphone charger and plug the device back in, and now it can't communicate. This is a well-known problem where if the TCP Server drops the connection, the client can't tell.

Since my software never initiates communication with the devices (it only reacts to incoming data), I can't tell if the connection has been broken.

What I've done, then, is given them a 'reconnect to device X' button on their software.

The problem is that I can't de-reference the TcpClient in my software. I need to reconnect to the TCP Server without losing the reference to the original TcpClient object and I'm having a tough time figuring out how to do that.

Closing the underlying client ( TcpClient.Client.Close() ) followed by a TcpClient.Connect(values) doesn't reconnect.

Is there any way to force a re-connect through a TcpClient without just disposing the initial TcpClient and creating a new one?

Thanks.
 

hanser

Ars Legatus Legionis
43,855
Subscriptor++
I think you're fixing the problem in the wrong place, because this sounds like a $13 hardware problem.

belkin-3-outlet.jpg
 

kilnborn

Ars Scholae Palatinae
700
I think you're fixing the problem in the wrong place, because this sounds like a $13 hardware problem.

belkin-3-outlet.jpg

I continue to be amused.

I've seen people ask the same question I've asked, on numerous places, including Stack Overflow, and the answers are always completely unrelated to the problem.

"How do I reconnect a TcpClient that has been closed on the server side?"

"This is how you determine if you need to reconnect a TcpClient."

--

That's not what I asked.

And here, I get a different answer, but also completely unrelated.

--

We're talking about factory workers. Pretend they do stupid crap, and unplug stuff they shouldn't.
 

krimhorn

Ars Legatus Legionis
39,865
[url=http:/civis/viewtopic.php?p=30682307#p30682307:2id1u8kq said:
kilnborn[/url]":2id1u8kq]
We're talking about factory workers. Pretend they do stupid crap, and unplug stuff they shouldn't.
Then you stick the plug end in a box to prevent that. Lots of experience in factories and, yeah, you've got to babysit a lot of them over stuff like this. Unplugging the production equipment such that it causes a problem is something that needs to be stopped.

Now, I do agree providing a 'reconnect' option is a good idea (even if I do think your specific problem is better served being fixed outside of software). However, it does not look like what you're asking for is possible (unfortunately, no documentation link).

Why is it that you can't dispose of the old TcpClient and instantiate a new one? If it's being created by a higher layer in the application stack then you can't do anything about the reconnection. The layer that the client is being instantiated in must provide you with a method to request a new TcpClient with a good connection.

As for not being able to tell when the server is down: you have a couple of options for keepalive/heartbeat style connection testing. That way, at the very least, you could log when it occurs (though it won't let you solve the connection problem yourself).
 

ComradeXavier

Ars Scholae Palatinae
882
Subscriptor++
[url=http:/civis/viewtopic.php?p=30677665#p30677665:2xm7udv7 said:
kilnborn[/url]":2xm7udv7]Closing the underlying client ( TcpClient.Client.Close() ) followed by a TcpClient.Connect(values) doesn't reconnect.

Is there any way to force a re-connect through a TcpClient without just disposing the initial TcpClient and creating a new one?
I've been reading up on Socket and TcpClient and UdpClient in the last few days. As far as I can tell, the objects are designed for a single use. You new, connect, disconnect, dispose, repeat. It makes sense when you consider the underlying OS sockets API. When you call closesocket(s) you can't then call connect(s) again; you have to call socket() to get a new socket handle. An instance of Socket behaves (mostly) like one lower-level socket handle; TcpClient and UdpClient are just specializations to hide some of Socket's complexity that isn't required in ordinary TCP and UDP sessions.
 

hanser

Ars Legatus Legionis
43,855
Subscriptor++
[url=http:/civis/viewtopic.php?p=30682307#p30682307:228l8n1p said:
kilnborn[/url]":228l8n1p]That's not what I asked.

And here, I get a different answer, but also completely unrelated.
Just making sure you don't have tunnel vision. ;)

We're talking about factory workers. Pretend they do stupid crap, and unplug stuff they shouldn't.
I've worked in manufacturing cleanroom as a non-skilled technician. Today I work in a group that is literally called "manufacturing software". In general, if your production facilities allow this kind of error to occur, they're not set up properly. I know that's beyond the scope of what you do, but it sounds like the TPTB should probably at least read The Toyota Way.

This outlet problem would fall under either the seiton category of problems, and reflects an ignorance of seiri and a work environment that's never heard of a poka-yoke. Your dismissive references to the people that actually work at the genba, and indicates an organization that wouldn't know jidoka if it slapped them in the face.

Yes, I am intimately familiar with manufacturing environments and the problems that are unique to making physical objects. I know how to build organizations that continuously self-improve, and treat their people as contributors that are able to add value instead of overgrown children that must somehow be managed.

Unskilled factory workers aren't dumb. They're just treated that way by organizations that don't know any better.

--

Edit: This came across more harshly than I wanted. I understand you have problems to solve, and you can't single-handedly change what is a pretty normal western manufacturing culture. There are lots of interesting software problems to work on in the manufacturing space, but you don't get to work on them when you're busy solving lowest-common-denominator stuff. Your culture is literally holding you back from working on more fun, more interesting, and ultimately more valuable problems. How exactly does this particular problem contribute to your customer value proposition? There is a virtuous feedback loop between work that adds value and work that is interesting.

I like manufacturing. I like the software problems in manufacturing. It gets me down when I see/hear about people solving problems that shouldn't even exist in this space, because it can be so much more.

Also, we're hiring.
 

kilnborn

Ars Scholae Palatinae
700
[url=http:/civis/viewtopic.php?p=30677665#p30677665:2bte25ns said:
kilnborn[/url]":2bte25ns]Closing the underlying client ( TcpClient.Client.Close() ) followed by a TcpClient.Connect(values) doesn't reconnect.

Is there any way to force a re-connect through a TcpClient without just disposing the initial TcpClient and creating a new one?
I've been reading up on Socket and TcpClient and UdpClient in the last few days. As far as I can tell, the objects are designed for a single use. You new, connect, disconnect, dispose, repeat. It makes sense when you consider the underlying OS sockets API. When you call closesocket(s) you can't then call connect(s) again; you have to call socket() to get a new socket handle. An instance of Socket behaves (mostly) like one lower-level socket handle; TcpClient and UdpClient are just specializations to hide some of Socket's complexity that isn't required in ordinary TCP and UDP sessions.

This turns out to be correct. Once a device powers down, and disconnects, I can't use the same TcpClient to do anything once it gets powered up and is now re-accepting connections.

The solution I ended up going with was to strip out the discrete device reconnect from the UI, and add a global reconnect. This strips out all references I have to all of the TcpClient objects, and reinitializes them.

I'd much prefer to handle UDP communications, but oh well.

--

I don't have control over anything but the software at my clients' facilities. I can't make them make it hard to unplug crap. If they make it easy to unplug crap, my job is to make it easy to reconnect. I can recommend things, but they'll typically get bogged down in accounting, and just stay there.
 
Status
Not open for further replies.