Reconnect TCPClient after interruption

 ̄綄美尐妖づ 提交于 2019-12-01 23:42:26

do I need to instantiate a New TcpClient in the Client Apps, whenever such interruption occur?

Yes. If the connection represented by the TcpClient is broken, you can't use that object for further communicating, nor can you connect it again. Create a new TcpClient object.

Your problem is likely that a NAT gateway times out your TCP connection, so nothing can get through between your server<->client, if all your client does is read from the connection, it will not discover this case, and it thinks the connection is still open.

I have typically solved this problem before in the past by using threads. I created a control thread that loops through and does administrative things like, check the connection, check the latest user input, check a server shutdown request, etc. When it is done it slept for half a second and did it all over again.

I then had a seperate Socket thread that was created and simply maintained a network socket. It would open the connection once started and loop repeatedly looking for incoming messages from whoever it was connected to. If it found a message it would process it and store it in a volatile object collection for consumption by the control thread. If something happened to the connection it would automatically try to resolve and if it couldn't it would go into a 'dead' state. The control thread at its next iteration would clean up the dead socket threads and create new ones as necessary.

Debugging was a nightmare but it was surprisingly stable once the bugs were worked out. On one instance it ran successfully for over a year with tens of thousands of connections and hundreds of concurrent connections with no need for restarts or any maintenance whatsoever.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!