c - 使用 tcp ://, 时,不要使用 epgm ://do. 接收消息 为什么?

标签 c zeromq pgm-protocol

我尝试使用 ZeroMQ 指南中的天气示例,使用 epgm:// 传输类。一开始我的代码:

// Publisher

#include "zhelpers.h"

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

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

        //  Send a message to all subscribers
        char update [20];
        sprintf ( update, "%05d %d %d", zipcode, temperature, relhumidity );
        s_send ( publisher, update );
    }
    zmq_close ( publisher );
    zmq_ctx_destroy ( context );
    return 0;
}

    // Subscriber
    #include "zhelpers.h"

    int main ( int argc, char *argv [] )
    {
        //  Socket to talk to server
        printf ( "Collecting updates from weather server…\n" );
        void *context = zmq_ctx_new ();
        void *subscriber = zmq_socket ( context, ZMQ_SUB );
        // int rc = zmq_connect ( subscriber, "tcp://192.168.1.4:5556" );
           int rc = zmq_connect ( subscriber, "epgm://192.168.1.9;224.0.0.251:5556" );
        assert ( rc == 0 );

        //  Subscribe to zipcode, default is NYC, 10001
        char *filter = ( argc > 1 )? argv [1]: "10001 ";
        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 < 100; update_nbr++ ) {
            char *string = s_recv ( subscriber );

            int zipcode, temperature, relhumidity;
            sscanf ( string, "%d %d %d",
                    &zipcode, &temperature, &relhumidity );
            total_temp += temperature;
            free ( string );
        }
        printf ( "Average temperature for zipcode '%s' was %dF\n",
            filter, (int) ( total_temp / update_nbr ) );

        zmq_close ( subscriber );
        zmq_ctx_destroy ( context );
        return 0;
    }
  1. 我已经在同一台主机上用 tcp:// 试过这段代码(见评论),它工作正常。
  2. 我用这个例子尝试了多播地址Openbook Linux (使用我的多播地址),我收到了消息。
  3. 我使用 pgm:// 安装了 ZeroMQ(在我遇到断言错误之前)。
  4. 我的主机都是Ubuntu 16.04系统
  5. 我正在使用 ZeroMQ 4

我的问题是我没有通过 epgm:// 收到任何消息。两个程序都在运行,但没有收到任何消息。我错过了什么?我不明白可能出了什么问题。

最佳答案

对于问题的后来访问者,代码很好并且可以运行。要查看它是否适用于您的系统,请将 for 循环更改为一个循环并将发布者中的邮政编码设置为 10001,如果您在没有参数的情况下启动订阅者。

关于c - 使用 tcp ://, 时,不要使用 epgm ://do. 接收消息 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46820308/

相关文章:

c - 当 main() 调用 main() 时堆栈帧会发生什么

c - 关于定义指令参数的简单代码混淆

java - ZeroMQ 运行服务器 Java

c# - 失去网络连接时 ZMQ 发布-订阅程序失败

c++ - 如何正确理解ZeroMQ的ZMQ_RCVHWM选项?

c# - 非管理员帐户的 Windows PGM 套接字访问错误

tcp - 我可以将 ZeroMQ 与基于软件的负载均衡器 HAProxy 一起使用吗?

c# - 透明Windows窗体,内部有OpenGL绘图

c - 在 C 中将单个文件拆分为多个文件 - 性能方面

c# - 接收消息队列消息而不从接收队列中读取它们?