[英]Sorting a client heartbeat from an actual data return?
So in a nutshell, I have server set up using tcplistener / tcpclient, and it has a couple dozen client machines. 简而言之,我已经使用tcplistener / tcpclient设置了服务器,并且它有几十个客户端计算机。 Everything is working well, except that I do not yet have a system set up to detect whether or not a client has disconnected. 一切工作正常,除了我还没有设置系统来检测客户端是否断开连接。 I would like to use heartbeats (periodically sent packets) to show the client is still there. 我想使用心跳(定期发送的数据包)来显示客户端仍然存在。 Unfortunately, what is happening is that the server has a waiting receive block called so that it can get actual data from the clients, but it is receiving the heartbeats instead. 不幸的是,正在发生的事情是服务器有一个等待接收的调用块,以便它可以从客户端获取实际数据,但它接收的是心跳信号。 Is there any way I can set up another receive on the same port that is looking specifically for heartbeats? 有什么方法可以在同一端口上设置另一个专门用于心跳的接收器? -Thanks. -谢谢。
One common way is to define your packets to have a specific type, therefore the server packet receiving code can just check the type and deal with it appropriately. 一种常见的方法是将您的数据包定义为具有特定类型,因此服务器数据包接收代码可以检查该类型并适当地对其进行处理。 ie 即
if ( packet.type == PacketTypes.HeartBeat )
//keep alive the client
else
//it's data (or another packet)
When you add your code I can give a better example. 当您添加代码时,我可以举一个更好的例子。
UPDATE: 更新:
As per our discussions in the comments, you can simply create a separate thread for dealing with your heartbeat packets. 根据评论中的讨论,您可以简单地创建一个单独的线程来处理您的心跳数据包。 In this thread, your socket receive code will ignore anything but heartbeat packets by first checking the type. 在此线程中,您的套接字接收代码将通过首先检查类型来忽略除心跳数据包以外的任何内容。
You can also create another thread housing a socket receive on the same port to deal with your data. 您还可以创建另一个线程,该线程容纳在同一端口上接收的套接字以处理数据。 In this case, it would just ignore any heartbeat packets received. 在这种情况下,它将仅忽略接收到的任何心跳数据包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.