且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我要如何检查是否TcpClient的连接?

更新时间:2023-02-05 23:24:57

有那么多的东西,可以通过套接字打破通信,其中有许多是完全的控制范围之内,***保持它的简单。

There are so many things that can break communications over a socket, many of which are totally outside your control, best keep it simple.

目前,你有一个完全可以接受的检测机制,死简单,成本低。

At the moment you have a perfectly acceptable detection mechanism, dead simple and low cost.

您可以做一个心脏的跳动,从客户端发送一个我在这里的消息每隔X出头,然后通过在服务器运行,看看是否有没有过一个用于X *小提琴因子区间。使用的处理器时间和带宽虽然。

You could do a heart beat, send an I'm here message from the client every X somethings, and then have the server run through and see if there hadn't been one for X * fiddle factor intervals. Uses processor time and bandwidth though.

另一种选择取决于您的客户端上检测它不再连接,并请求一个新的连接。如果你有在请求一个唯一的标识符,您可以通过现有的连接上运行它,杀死它,如果它的存在,并开始再继续正常。

Another option depends on your client detecting it's no longer connected and requesting a new connection. If you have a unique identifier in the request , you can run through your existing connections for it, kill it, if it's there, and start then continue as normal.

我知道除了感觉不对,但它只是一个真正的问题,如果你继续失去连接,如果你是你的努力将更好地整理了这一点执导。

I know exception doesn't feel right, but it's only really a problem if you keep losing the connection, and if you were your efforts would be better directed at sorting that out.