如何在客户端以外的IP上使用ZMQ

时间:2016-06-08 15:37:33

标签: zeromq pyzmq jzmq

我了解了如何在ZeroMQ上使用 localhost ,但我未能在远程IP上执行此操作。

Q1: 我是否需要经纪人?
如果是这样, Q2: 哪个经纪人以及如何做它。?

更新

行。我使用ZMQ天气更新示例,但使用远程IP(不是localhost)。以下是我使用C# ZMQ绑定所做的事情(但是,我可以使用任何其他语言):

ZMQ 服务器:

using (var context = new ZContext())
using (var publisher = new ZSocket(context, ZSocketType.PUB))
{
    string address = "tcp://*:5001";

    publisher.Bind(address);

    publisher.Send("msg")
}

代理:

using (var context  = new ZContext())
using (var frontend = new ZSocket(context, ZSocketType.XSUB))
using (var backend  = new ZSocket(context, ZSocketType.XPUB))
{
    // Frontend is where the weather server sits
    string localhost = "tcp://127.0.0.1:5001";
    Console.WriteLine("I: Connecting to {0}", localhost);

    frontend.Connect(localhost);

    // Backend is our public endpoint for subscribers
    string remoteIP = "216.123.23.98";                          // For example
    var tcpAddress = string.Format("tcp://{0}:8100", remoteIP); // I also tried localhost address here

    Console.WriteLine("I: Binding on {0}", tcpAddress);

    backend.Bind(tcpAddress);

    var epgmAddress = string.Format("epgm://localhost;{0}:8100", remoteIP);

    Console.WriteLine("I: Binding on {0}", epgmAddress);

    backend.Bind(epgmAddress);

    using (var subscription = ZFrame.Create(1))
    {
        subscription.Write(new byte[] { 0x1 }, 0, 1);
        backend.Send(subscription);
    }

    // Run the proxy until the user interrupts us
    ZContext.Proxy(frontend, backend);
}

客户端:

using (var context    = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
    string remoteIP = "tcp://216.123.23.98";              //For example
    Console.WriteLine("I: Connecting to {0}…", remoteIP);

    subscriber.Connect(connect_to);

    // Subscribe to zipcode
    string zipCode = args[0];
    Console.WriteLine("I: Subscribing to zip code {0}…", zipCode);

    subscriber.Subscribe(zipCode);

    // Process 10 updates
    int i = 0;
    long total_temperature = 0;
    for (; i < 20; ++i)
    {
        ZError err;
        using (var replyFrame = subscriber.ReceiveFrame(out err))
        {
            string reply = replyFrame.ReadString(Encoding.ASCII);

            Console.WriteLine(reply);
            total_temperature += Convert.ToInt64(reply.Split(' ')[1]);
        }
    }
    Console.WriteLine("Average temperature for zipcode '{0}' was {1}", zipCode, (total_temperature / i));
}

当我运行此操作时,我在服务器中出错,代理中出错 - 服务器获取

  

Invalid end point

和代理获取EINVAL(22)

  

Invalid argument at ZeroMQ.ZSocket.Bind(String endpoint)

1 个答案:

答案 0 :(得分:1)

A1: 不,ZeroMQ是一个无经纪人的消息传递框架。

A2: N / A

如何修复代码?

对于TCP / IP案例,所有服务都需要遵守相应的传输类寻址规则 - .bind() / .connect() 方法必须说明IP:PORT#规范的两个部分(对IP - 部分的DNS解析有一些帮助,但:PORT# - 部分仍然是强制性的)

(源代码在客户端中无法满足,参考号:

 subscriber.Connect(connect_to);

然而,对于正确的Proxy,还应该指定:PORT# - 侧匹配 :8100 ,即.connect()。< / p>

为了清晰起见并避免 port# -collision,请从代码中删除 epgm 传输类。