简体   繁体   English

等待连接的正确方法是什么?

[英]What is the proper way to wait for connections?

I am trying to implement a simple message passing between two applications using NetMQ (a slightly more elaborate description of what I am trying to achieve is below). 我正在尝试使用NetMQ实现在两个应用程序之间传递的简单消息(稍微更详细地描述了我想要实现的内容)。
After a bit of trial and error I've found that I can't just send or receive messages right away after a Connect/Bind calls, since they are non blocking and actually return even if the connection hasn't been established yet. 经过一些试验和错误后,我发现在Connect / Bind调用之后我不能立即发送或接收消息,因为它们是非阻塞的,即使尚未建立连接也会实际返回。
For now I solved this with Thread.Sleep(), but this has a bad taste to it and definitely a no-go for a production system. 现在我用Thread.Sleep()解决了这个问题,但这对它有不好的品味,对于生产系统来说绝对不行。

So the question is, how's one supposed to do it in NetMQ/ZeroMQ? 所以问题是,如何在NetMQ / ZeroMQ中做到这一点?

Client example: 客户端示例:

        using (NetMQContext ctx = NetMQContext.Create())
        {
            using (var client = ctx.CreatePushSocket())
            {
                client.Connect("tcp://127.0.0.1:5555");
                Thread.Sleep(100); // wait for connection

                for (int i = 0; i < 5; i++) 
                {
                    client.Send("test " + i , true);
                }
            }
        }
    }

Server example: 服务器示例:

    using (NetMQContext ctx = NetMQContext.Create())
    {
        using (var server = ctx.CreatePullSocket())
        {
            server.Bind("tcp://127.0.0.1:5555");
            Thread.Sleep(100); // wait for connection
            while (true)
            {
                var str = server.ReceiveString();
                Console.Out.WriteLine(str);
                Thread.Sleep(60*1000); // do msg processing
            }
        }
    }

Description of what I am trying to achieve: 我想要实现的目标的描述:

Client - Sends messages to a single server. 客户端 - 将消息发送到单个服务器。 The client should not block and should not discard messages when server is not available. 当服务器不可用时,客户端不应阻止并且不应丢弃消息。 The client can come offline/online at any time. 客户端可以随时离线/在线。

Server - Receives messages from a single client. 服务器 - 从单个客户端接收消息。 The server blocks until a message is received. 服务器将阻塞,直到收到消息。 Server needs to do lengthy processing of the message and should not loose any other messages while processing. 服务器需要对消息进行冗长的处理,并且在处理时不应丢失任何其他消息。 The server can come offline/online at any time. 服务器可以随时离线/在线。

both receive and send can wait until can be executed, you passed true to dontWait parameter on your example, just remove it and it will send the message when it can. 接收和发送都可以等到可以执行,你在你的例子中传递了真实的dontWait参数,只需删除它,它会尽可能地发送消息。

For the receive you don't have to sleep because it will wait until message is available. 对于接收,您不必睡觉,因为它会等到消息可用。

As suggested using Poller is also a solution (you can poll when the socket can send and when messages are ready to be consumed), take a look at the testing for poller class: https://github.com/zeromq/netmq/blob/3.3.3-rc5/src/NetMQ.Tests/PollerTests.cs . 正如使用Poller建议的那样也是一个解决方案(你可以在套接字发送时轮询以及消息准备好被消费时),看一下poller类的测试: https//github.com/zeromq/netmq/blob /3.3.3-rc5/src/NetMQ.Tests/PollerTests.cs

The best solution to your sleep on the server side is to create a socket poller and poll on the pull socket until a message is received. 在服务器端睡眠的最佳解决方案是在拉出套接字上创建套接字轮询器并轮询,直到收到消息。 This avoids wasteful sleeps, and makes for generally tighter code. 这避免了浪费的睡眠,并且使得代码通常更紧凑。

On the client side the best solution is probably to create two sockets (one for sending messages, one for receiving), and have the server announce its presence ready for the client to send the message. 在客户端,最好的解决方案可能是创建两个套接字(一个用于发送消息,一个用于接收),并让服务器宣布其存在,以便客户端发送消息。 Because ZeroMQ is efficient at handling multiple connections, this solution will work very well. 由于ZeroMQ可以有效地处理多个连接,因此该解决方案可以很好地工作。

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

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