zmq

Linux下使用ZMQ实践“发布-订阅”模型

匿名 (未验证) 提交于 2019-12-02 21:59:42
一、背景 二、相关知识 2.1 ZMQ_PUB ZMQ_PUB A socket of type ZMQ_PUB is used by a publisher to distribute data. Messages sent are distributed in a fan out fashion to all connected peers. The zmq_recv(3) function is not implemented for this socket type. When a ZMQ_PUB socket enters the mute state due to having reached the high water mark for a subscriber, then any messages that would be sent to the subscriber in question shall instead be dropped until the mute state ends. The zmq_send() function shall never block for this socket type. ZMQ_PUB为发布端socket类型,用于消息分发,消息以扇出的方式分发到各个连接端上。该socket类型仅支持zmq_send进行发送

zmq利用protobuf通信

元气小坏坏 提交于 2019-11-27 07:16:25
protobuf序列化之后为二进制数据,数据中可能包含 ‘\0’,直接转换为char *类型会导致发送数据不完整。 解决方法: void buildProtobufMsg(const string& test,zmq::message_t *msg) { if (msg == nullptr) { return; } Test::ProtoBufData data; data.set_test(test); int size = data.ByteSize(); char arr[size]; data.SerializeToArray(arr,size); msg->rebuild(size); memcpy(msg->data(),arr,size); } 切记不可将序列化之后的数据转成char * 发送,否则导致数据不完整。 调用: zmq::message_t msg; buildProtobufMsg(string("hello“),&msg); zmqsocket.send(msg); 【转】: https://blog.csdn.net/bubbleyang/article/details/82628059 来源: https://www.cnblogs.com/hshy/p/11350314.html