同步 TCP 消息

发布于 2024-12-10 14:59:48 字数 129 浏览 0 评论 0原文

我至少有 3 个 TCP 客户端,每个客户端都有一个线程。我正在发送消息并等待答案,但有时我必须等待接收所有客户端的响应,这取决于服务器发送的消息类型。我已经向客户端发送消息并接收消息,但是当我必须等待其他客户端响应时,我直到现在才可以这样做。

I have minimum 3 TCP client, each has a Thread. I'm sending out messages and waiting for the answer, but sometimes I have to wait to receive the response from all client, this is depending what kind of message sent the server out. I already made to send messages to the clients and receiving, but when I have to wait for the other client response I couldn't do that until now.

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

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

发布评论

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

评论(1

燕归巢 2024-12-17 14:59:49

至于您没有提到您的环境/语言,我假设 C#/.NET 4

您需要一种机制让每个客户端发出响应到达的信号。这通常是通过 AutoResetEvents 完成的:每个客户端将其响应发送回服务器。服务器本身可以从客户端发送的响应(或任何其他属性,例如连接)中提取它。然后他设置适当的 AutoResetEvent。

先前发起发送消息的线程随后可以等待所有 AutoResetEvents 被设置。

As far as you didn't mention your environment/language, I assume C#/.NET 4

You need a mechanism for each client to signal the arrival of a response. This is usually done with AutoResetEvents: Each client sends his response back to the server. The server itself can extract from the reponse (or any other property, e.g. the connection) with client has sent it. Then he sets the apporpriate AutoResetEvent.

The thread that formerly initiated sending the message can afterwards wait for all AutoResetEvents to be set.

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