简体   繁体   English

更新队列并从队列中删除所需的内容

[英]Updating Queue and deleting desired contents from the Queue

I am stuck in a really confusing situation here therefore, I am going to explain the whole scenario. 因此,我陷入了一个非常令人困惑的情况,因此,我将解释整个情况。

I have developed a UDP Server/ multiple Client appplication. 我已经开发了UDP服务器/多客户端应用程序。

The Server has a main Receiving Thread. 服务器有一个主接收线程。 Each Client which wants to communicate with the Server first sends a Message with some ID to the Server. 每个要与服务器通信的客户端首先向服务器发送一条带有某些ID的消息。 Server receives this Message from the Client. 服务器从客户端收到此消息。 Server has a text file containing list of authorized IDs. 服务器有一个包含授权ID列表的文本文件。 When a Message is received from the client, server compares the ID received from the client with the IDs available in its text file. 从客户端收到消息后,服务器会将从客户端收到的ID与其文本文件中可用的ID进行比较。 If client's ID matches with any ID in Server's text file, that client is considered as authenticated and a new thread alongwith a new socket is created dedicated for this client. 如果客户端的ID与服务器的文本文件中的任何ID匹配,则该客户端将被视为已认证,并且将为此客户端专门创建一个新线程以及一个新套接字。 The information of this new client is also updated in a Queue containing information of all active clients. 此新客户端的信息也会在包含所有活动客户端信息的队列中更新。

All this task is completed by now Now the issue is that since each client will have a dedicated thread serving it, I want these threads to send some message to their respective clients and wait for response from the client, if no response is received from the client within a specific time, the client is considered dead and is removed from the Queue in the main Receieving Thread. 现在,所有这些任务都已完成。现在的问题是,由于每个客户端都将有一个专用的线程为它服务,所以我希望这些线程向各自的客户端发送一些消息,并等待客户端的响应(如果未从客户端收到响应)。客户端在特定时间内被视为已死,并从主接收线程中的队列中删除。

But the problem is that how will "all" x number of clients will tell the main receieivng thread that their respective client is INACTIVE? 但是问题在于,“全部” x个客户将如何告诉主接收线程其各自的客户不活跃? If this part is done, will the main Receiving Thread have to traverse the whole Queue, find out the client's information which is dead and then how will this be deleted? 如果完成了这部分,主接收线程是否必须遍历整个队列,找出已死的客户端信息,然后将其删除? this is really confusing situation for me. 这对我来说真是令人困惑。

It has been so long but I am unable to keep a recored of active clients in my application. 已经很长时间了,但是我无法在我的应用程序中保留活动客户端的记录。

Please help me asap. 请尽快帮我。 Thanks in advance :) 提前致谢 :)

EDIT: Another idea could be that the Queue has x number of elements and the satus of each client. 编辑:另一个想法可能是该队列具有x数量的元素和每个客户端的饱和状态。 eg: initially for x number of clients: 例如:最初为x个客户:

Client#    Status

1            0

2            0

3            0

.            .

.            .

.            .

.            .

x            0

Whenever a new Client becomes Active, Status is changed from 0 to 1 and whenever the client goes down, status is changed back form 1 to 0. 每当新的客户端变为活动状态时,状态就会从0更改为1,并且每当客户端关闭时,状态都会从1更改回0。

Does this seem to be correct approach? 这似乎是正确的方法吗?

I better advice you to use Tcp . 我最好建议您使用Tcp。 Using UDP you are obliged to do things by yourself . 使用UDP,您必须自己完成操作。

I want these threads to send some message to their respective clients and wait for response from the client 我希望这些线程向其各自的客户端发送一些消息,并等待客户端的响应

First: Make Queue or List thread safe 第一:使队列或列表线程安全

Pass the id (index) of the List where you storing information about client address ( client sockaddr) and other informations like state . 传递列表的ID(索引),在其中存储有关客户端地址(客户端sockaddr)的信息以及其他信息(如state)。 Inside threadand use index to obtain info about client .obtaining client addr u can send message to it . 在线程内部并使用索引获取有关客户端的信息。获取客户端地址可以向其发送消息。

If this part is done, will the main Receiving Thread have to traverse the whole Queue, find out the client's information which is dead and then how will this be deleted? 如果完成了这部分,主接收线程是否必须遍历整个队列,找出已死的客户端信息,然后将其删除? this is really confusing situation for me. 这对我来说真是令人困惑。

You will traverse list and obtain index of client and change state . 您将遍历列表并获取客户端索引并更改状态。 in client thread when you will see that client state not changed to active within specific time you will delete that client info from list or queue. 在客户端线程中,当您看到客户端状态在特定时间内未更改为活动状态时,将从列表或队列中删除该客户端信息。

Happy programmings 快乐编程

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM