使用c#从服务器向多个客户端广播消息

发布于 2024-11-30 09:40:09 字数 221 浏览 1 评论 0原文

场景:想要从服务器向多个客户端广播消息。

需要:我有一个服务器和连接到该服务器的多个客户端。如果管理员想要更新服务器,那么服务器将向所有连接的客户端广播消息“请注销...服务器正在更新”。

如果有人对消息广播有任何想法,请尽快回复。我正在使用 TcpServer

语言:c#

Scenario: Want to braodcast message to multiple clients from server.

Need: I have a server and multiple clients connected to that server. If Admin want to update server then server will broadcast message to all the clients connected that "Please logout...server is being updated".

Please if any one have any idea regarding the message broadcasting reply asap. I am using TcpServer

Language : c#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心空情空意 2024-12-07 09:40:09

对于 TCP,我使用从主服务器协议线程初始化的线程;当需要发送到每个客户端的主数据结构发生更改时,会设置一个静态布尔值并将相关数据发送到客户端,该客户端还有一个在主客户端协议线程之外实现的线程来侦听和处理实施这些改变。

我使用令牌传递系统来锁定对数据结构的访问,因此客户端只能在调用其令牌时进行更改。我标记每个令牌,以便在每个用户收到要更改的数据之前静态变量不会设置为 false。

这比使用一行代码更具逻辑挑战。无论如何,任何库接口都必须使用类似的逻辑,因为它必须联系 (n) 个客户端。

就我而言,这种用法减少了客户端调用,并允许仅传递已更改的数据。与传递整个数据结构的初始加载不同,但我做了类似的事情,也与遍历数据结构并将其保存到硬盘有关。这意味着减少通过网络传递的数据,并使主协议线程保持更长时间的开放状态,以便那些真正需要访问的人能够获取它。

编辑:为了没有人抱怨,我直接谈论 TCP/IP,而不是需要建立连接的 UDP。 UDP 确实使用了多播概念,并且设置起来相当简单。请注意,它很容易存在安全风险,即使它确实提供了一定的效率,但它的使用范围应该被视为有限。

For TCP I use a thread that is initialized from the main server protocol thread; when a change occurs to the main data structure, which needs to be sent to each client, a static boolean is set and sends that relevant data to the client, which also has a thread that is implemented off the main client protocol thread to listen and implement those changes.

I use a token passing system to lock access into the data structure, so a client can only make changes when their token is called. I tag each token, so that the static var isn't set to false until each user has received the data to be changed.

This is more a challenge in logic than utilizing a single line of code. Regardless any library interface would have to use similar logic in the sense it must contact (n) number of clients.

In my case, this usage reduces client calls and allows for the passing of only data that has been changed. Unlike an initial load where the entire data structure is passed, but I do something similar that is also connected to traversing the data structure and saving it to hard disk. This means a reduction in data that is passed over the network and keeps the main protocol thread open longer so that those that really need access get it.

edit: so that no one complains, I'm talking straight TCP/IP and not UDP, where the connection needs to be established. UDP does use a multicast concept and setting it up is rather easy. Beware, it is prone to being a security risk, and even though it does provide some efficiency, it's scope of use should be viewed as limited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文