2

这不需要工作吗?

#include <czmq.h>

zsock_t *sockout = zsock_new_pub("inproc://a");
zsock_set_sndhwm (sockout, 20);

如何设置 HWM 和/或 BUF 大小?

更新:我添加了更多代码并在这种情况下工作:

#include <string>
#include <czmq.h>

int main (void){

    zsock_t *sockout = zsock_new_pub("inproc://a");
    zsock_set_sndhwm (sockout, 20);
    std::string data2send;
    for (size_t i = 0; i < 1000; i++){
        data2send = "data" + std::to_string(i);
        zsock_send(sockout, "s", data2send.c_str() );
    }
}

虽然zsock_set_sndhwm (sockout, 20);在这个特定的上下文中工作。我必须弄清楚它不起作用的上下文。

4

1 回答 1

0

我正在回答我自己的问题以获得完整的解决方案 pub/rec

发布者代码:

#include <string>
#include <czmq.h>

int main (void)
{
    zsock_t *sockout = zsock_new_pub("ipc://a");
    zsock_set_sndhwm (sockout, 20);
    zsock_set_rcvhwm (sockout, 20);

    std::string data2send;
    for (size_t i = 0; i < 1000; i++){
        data2send = "data: " + std::to_string(i);
        sleep(1);
        zsock_send(sockout, "s", data2send.c_str() );
    }
}

订阅者(接收者)代码:

#include <string>
#include <czmq.h>
#include <iostream>

int main (void){
    zsock_t *sockin = zsock_new_sub("ipc://a", "");
    zsock_set_sndhwm (sockin, 20);
    zsock_set_rcvhwm (sockin, 20);

    char *m;
    std::string m_str;

    while(true){
        zsock_recv(sockin, "s", &m);
        std::cout << "**READ: " << m << "\n\n";
    }
}
于 2021-09-30T13:10:19.113 回答