简体   繁体   English

如何知道服务器连接在客户端丢失

[英]How to know server connection is lost in client side

I have a tcp server that writes in winforms and a client in android. 我有一个用winforms编写的tcp服务器和一个android中的客户端。 I connect devices to a wirless network then I disconnect server from this network. 我将设备连接到无线网络,然后断开服务器与该网络的连接。 However client continue to listen server for a while then it closes its socket. 但是客户端继续监听服务器一段时间,然后关闭其套接字。 How can I tell client to close socket when server is disconnected from network? 当服务器与网络断开连接时,如何告诉客户端关闭套接字?

The reason it happens (if I understand the problem correctly) is that there is not inherent way to know that a connection has been closed, unless you try to send a package. 发生这种情况的原因(如果我正确理解了问题)是没有固有的方式知道连接已关闭,除非您尝试发送程序包。 So in your client, if you try to send an empty package over the connection, it will immediately report if the connection has been closed. 因此,在客户端中,如果您尝试通过连接发送空包,它将立即报告连接是否已关闭。

This is the reason that Heartbeats exist, and you can configure your socket to use them, or you can have your client periodically (or when needed) attempting to send an empty package and report the status of the connection. 这就是存在心跳的原因,您可以配置套接字以使用它们,或者可以让客户端定期(或在需要时)尝试发送空包并报告连接状态。

There is an excellent article on CodeProject about this, see here . 在CodeProject上有一篇关于此的出色文章, 请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命名管道:服务器端如何知道客户端已断开连接? - Named pipes: how server side can know that client has disconnected? 如何在服务器端获取signalR客户端的连接ID? - How to obtain connection ID of signalR client on the server side? 如何从客户端知道表是否已更新 - How to know from client side if table was updated 客户端应用程序与服务器应用程序之间的UDP'连接'丢失 - Lost UDP 'connection' between client app and server app SL4,EF,Ria服务-当在服务器端发生删除或没有发生删除时,客户端如何知道发生了什么? - SL4, EF, Ria Services - When a deletion happens or doesn't happen on the Server Side, how does the Client Side know what happened? 如何在服务器端侦听客户端事件 - How to listen to Client Side events on Server Side 如何知道客户端调用连接是否在TcpListener上关闭? - How to know if the client call connection close on TcpListener? 如何使客户端自动检测WCF服务是否关闭或连接断开 - How to make client automatically detect if WCF service is down or lost connection 如何从客户端应用程序中的WCF服务检测连接丢失 - How to detect Connection Lost from WCF Service in client app C#套接字-如何检测我的客户端是否失去连接 - C # Socket - How to detect if my client has lost connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM