具有PHP订阅者的ZeroMQ pub / sub无法从C#中的发布者接收消息

时间:2014-07-29 09:53:01

标签: c# zeromq

我正在使用ZeroMQ来促进我需要的发布/订阅环境。 pub和sub都在localhost上运行。

我用C#实现了酒吧:

            var options = new Options();
            var parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
            if (!parser.ParseArguments(args, options))
                Environment.Exit(1);

            using (var ctx = ZmqContext.Create())
            {
                using (var socket = ctx.CreateSocket(SocketType.PUB))
                {                   
                    foreach (var endPoint in options.bindEndPoints)
                        socket.Bind(endPoint);

                    long msgCptr = 0;
                    int msgIndex = 0;
                    while (true)
                    {
                        if (msgCptr == long.MaxValue)
                            msgCptr = 0;
                        msgCptr++;
                        if (options.maxMessage >= 0)
                            if (msgCptr > options.maxMessage)
                                break;                        
                        if (msgIndex == options.altMessages.Count())
                            msgIndex = 0;
                        var msg = options.altMessages[msgIndex++].Replace("#nb#", msgCptr.ToString("d2"));                        
                        Thread.Sleep(options.delay);
                        Console.WriteLine("Publishing: " + msg);
                        socket.Send(msg, Encoding.UTF8);
                    }
                }
            }
在python中实现的

子:

def main():

    test_connect = "tcp://127.0.0.1:5000"
    test_topic = ""

    connect_to = test_connect
    topics = test_topic

    ctx = zmq.Context()
    s = ctx.socket(zmq.SUB)
    s.setsockopt(zmq.SUBSCRIBE, "")
    s.connect(connect_to)

    print "Receiving messages on All Topics ..."

    while True:
        print "try to receive"
        objA = s.recv()
        print objA

我首先运行sub,然后运行pub。但是sub不能从pub中接收任何消息。我不知道为什么。

我已经在python中使用pub进行了测试,而在php中使用了sub,在python中使用了pub / sub。他们俩都运作良好。但是当这个pub或sub被证明是用C#实现的时候,出现了问题。

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

按以下步骤

尝试 SUB -side
ctx = zmq.Context()              // aGlobalCONTEXT
s = ctx.socket(zmq.SUB)          // aSocketOBJECT
s.connect(connect_to)            // aSocketOBJECT.connect() ...make PUB/SUB relation
s.setsockopt(zmq.SUBSCRIBE, "")  // aSocketOBJECT.setsockopt() SUB to everything from this PUB

尝试 PUB - 在CLI上自我诊断/打印每次.Bind()尝试的结果(打印endPoint和&返回值(确认码) )来自socket.Bind()电话)

答案 1 :(得分:0)

尝试简化发布商,只是为了排查故障。我在找到的ZMQ Guide here中的第一个PUB示例中修改了以下内容:

using System;
using System.Text;
using ZeroMQ;

internal class Program
{
    public static void Main(string[] args)
    {
        using (var context = ZmqContext.Create())
        {
            using (ZmqSocket publisher = context.CreateSocket(SocketType.PUB))
            {
                // your first potential issue lies here, if you're not
                // populating your addresses properly then you're not going to
                // bind appropriately
                // Test by hard coding the address
                publisher.Bind("tcp://127.0.0.1:5000");

                int msgIndex = 0;

                while (true)
                {
                    // your second potential issue lies here, if your logic
                    // short circuits your send, that'll throw a wrench in the
                    // works
                    // Test by removing the logic and just sending a bunch of
                    // messages

                    var msg = "Message: " + msgIndex; // simplify
                    Console.WriteLine("Publishing: " + msg);
                    socket.Send(msg, Encoding.UTF8);
                    Thread.Sleep(500); // hard code
                    msgIndex++;
                    if (msgIndex > 1500) break; // hard code the break
                }
            }
        }
    }
}

......如果有效,那么你的逻辑就会出现问题。只需从那里构建并查看问题所在。如果失败,那么您的系统设置或ZMQ库可能存在问题。

相关问题