简体   繁体   English

根据实际数据返回排序客户心跳?

[英]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.

相关问题 TcpListener每5秒发送一次心跳并从客户端异步读取消息 - TcpListener send heartbeat every 5 seconds and read message from client async gRpc 发送心跳 - 客户端/服务器 - gRpc sending heartbeat - client/server 如何使用.net驱动程序从Neo4j返回实际数据? - How can I return actual data from Neo4j using the .net driver? 用于在服务器和客户端之间实现心跳的模式 - Pattern to implement heartbeat between server and the client 使用列表按字母顺序从MySQL排序数据 <string> 和方法在C#中返回排序列表 - sorting data from MySQL alphabetically using list<string> & Methods to return sorted list in C# 创建Web服务(WCF),该服务返回确认并通过调用客户端API返回实际结果 - Create Web Service (WCF) that return an acknowledgement and return the actual result by calling client API 从客户端获取控件的实际HTML代码 - Get The Actual HTML code for a control from client side WinForm 数据绑定控制结果返回 Class 名称不是实际数据 - WinForm Databound Control Results Return Class Name Not Actual Data 对数据库中的数据进行排序和分类 - Sorting and categorising data from database 从数组列表中排序数据 - Sorting of data from array list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM