同步 TCP 消息
我至少有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于您没有提到您的环境/语言,我假设 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.