ZeroMQ 的 EPGM 在天气 PUB-SUB 演示中不工作

ZeroMQ's EPGM not working in weather PUB-SUB demo

我已经用 openpgm 编译了 libzmq,在 windows 下没有任何变化。这里的代码取自 ZeroMQ Guide ("weather publisher" server/client)。但是,如果我将 "tcp" 更改为 "epgm",它将不再起作用(未收到数据,但已建立连接)。

void test_serv()
{    
    //  Prepare our context and publisher
    void *context = zmq_ctx_new();
    void *publisher = zmq_socket(context, ZMQ_PUB);
    int rc = zmq_bind(publisher, "epgm://127.0.0.1:5556");
    assert(rc == 0);

    //  Initialize random number generator
    srandom((unsigned)time(NULL));
    while (!stop_server)
    {
        //  Get values that will fool the boss
        int zipcode, temperature, relhumidity;
        zipcode = randof(1000) + 600;
        temperature = randof(215) - 80;
        relhumidity = randof(50) + 10;

        //  Send message to all subscribers
        char update[20];
        sprintf(update, "%d %d %d", zipcode, temperature, relhumidity);
        s_send(publisher, update);
    }
    LOG("END Server shutdown");
    Sleep(500);
    zmq_close(publisher);
    zmq_ctx_destroy(context);
}

void test_sock()
{    
    //  Socket to talk to server
    LOG("Collecting updates from weather server...");
    void *context = zmq_ctx_new();
    void *subscriber = zmq_socket(context, ZMQ_SUB);
    int rc = zmq_connect(subscriber, "epgm://127.0.0.1:5556");
    assert(rc == 0);

    //  Subscribe to zipcode, default is NYC, 10001
    char *filter = "1001 ";
    rc = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE,
        filter, strlen(filter));
    assert(rc == 0);

    //  Process 100 updates
    int update_nbr;
    long total_temp = 0;
    for (update_nbr = 0; update_nbr < 10; update_nbr++) {
        char *string = s_recv(subscriber);

        int zipcode, temperature, relhumidity;
        sscanf(string, "%d %d %d",
            &zipcode, &temperature, &relhumidity);
        total_temp += temperature;
        LOG(">> " << string);
        free(string);
    }
    LOG("Average temperature for zipcode "<< filter << "was " << (int)(total_temp / update_nbr) << 'F');

    zmq_close(subscriber);
    zmq_ctx_destroy(context);
}

我 运行 两个函数在不同的线程中,使用 tcp 任何东西都按预期工作。

我试过用 cmd.exe 做 "route print 0.0.0.0" 并使用接口 IP (192.168.137.64) 作为前缀而不是 "eth0",如 RFC 中所示:epgm://192.168.137.64 ;127.0.0.1:5556 on connect and/or 绑定,但这会破坏我的套接字并引发错误。

另外"PGM"需要管理员权限,我现在无法测试。

错误不是 "protocol not supported" errno 设置为 B (11),我不明白这是什么意思(没有相关文档)。

EPGM 有点挑剔。 According to this list post, if you're using EPGM your publisher and subscriber must be on separate hosts. More details here,看来这是ZMQ团队刻意的选择。

因此,尝试在不同的机器上启动您的 PUB 和 SUB(当然,相应地更改网络地址)。

原因可能是windows不支持环回捕获接口。我在 linux 上尝试了将协议更改为 epgm 的天气示例,它工作正常(好吧,显示了一些关于环回的警告,但消息传输正确)