zmq发布-订阅模式c++实现

2023-05-16

上一篇讲到zmq的安装及简单的请求-应答模式,本篇主要来看一下zmq的pub-sub代码如何实现。

发布-订阅模式的特点:

1.一个发布者可以被多个订阅者订阅,即发布者和订阅者是1:n的关系

2.当发布者数据内容发生变化时发布数据,所有订阅者均能及时收到数据。

发布者的代码:pub.cpp

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include "zmq.h"

int main()
{
    void* context = zmq_ctx_new();
    assert(context != NULL);

    void* publisher = zmq_socket(context, ZMQ_PUB);
    assert(publisher != NULL);

    int ret = zmq_bind(publisher, "tcp://*:5555");
    assert(ret == 0);

    int i = 0;
    while(1)
    {
        char szBuf[1024] = {0};
        snprintf(szBuf, sizeof(szBuf), "server i=%d", i);
        printf("pub ctx: server i = %d\n",i);
        ret = zmq_send(publisher, szBuf, strlen(szBuf) + 1, 0);
        i++;

        sleep(1);
    }

    zmq_close (publisher);
    zmq_ctx_destroy (context);

    return 0;
}

订阅者的代码:sub.cpp

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include "zmq.h"

int main()
{
    printf("Hello sub!\n");

    void* context = zmq_ctx_new();
    assert(context != NULL);

    void* subscriber = zmq_socket(context, ZMQ_SUB);
    assert(subscriber != NULL);

    int ret = zmq_connect(subscriber, "tcp://localhost:5555");
    assert(ret == 0);

    ret = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", 0);
    assert(ret == 0);

    while(1)
    {
        printf("enter while to sub ctx\n");
        char szBuf[1024] = {0};
        ret = zmq_recv(subscriber, szBuf, sizeof(szBuf) - 1, 0);
        if (ret > 0)
        {
            printf("%s\n", szBuf);
        }
    }

    zmq_close(subscriber);
    zmq_ctx_destroy(context);

    return 0;
}

特别注意:

1.我们在pub中bind,在sub中connect,在zmq的使用中无论是在pub还是sub中都可以bind,但是一般我们在pub中bind,在sub中connect 。反之sub端可能收不到消息

2.zmq_setsockopt – 设置zmq socket属性,sub端必须使用此方法,否则是收不到消息的。详细使用方法见官方文档:http://api.zeromq.org/3-2:zmq-setsockopt

3.pub端不能使用recv函数,sub端不能使用send函数

编译:

g++ -o sub sub.cpp -lzmq
g++ -o pub pub.cpp -lzmq

运行:先运行pub可能会使订阅者错过部分发布内容,要想订阅完整的内容需要先启动sub,我这里启动两个sub

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

zmq发布-订阅模式c++实现 的相关文章

随机推荐

  • go命令 build/run/install/env

    1 编译 xff1a go build o 指定生成文件的名字 go build o test exe main go xx go 2 直接运行程序 xff0c 不会编译成exe文件 1 go run go 3 安装程序 go env 4
  • go语言学习

    一 基础语法 1 变量定义 package main import 34 fmt 34 func main 定义变量 xff1a var 常量定义 xff1a const 01 先定义变量 xff0c 再赋值 xff0c var 变量名 数
  • python 检查变量类型_Python检查变量类型– 3种方式

    python 检查变量类型 In this tutorial we are going to learn how to check variable type in Python 在本教程中 xff0c 我们将学习如何在Python中检查变
  • GO学习每日一题

    注 xff1a 每日一题来源go语言官方公众号 day5 package main import 34 fmt 34 下面这段代码能否通过编译 xff0c 原因 答 xff1a 不能 invalid operation sm1 61 61
  • 用go语言实现查找两个数组的异同

    用go语言实现查找两个数组的异同
  • golang 计算本周周一日期

    func GetThisWeekFirstDate thisMonday int64 now 61 time Now offset 61 int time Monday now Weekday if offset gt 0 offset 6
  • Golang 获取今天和昨天零点的时间

    获取今天和昨天零点的时间 func GetTodayAndYesterdayZroe now 61 time Now today 61 time Date now Year now Month now Day 0 0 0 0 time Lo
  • Golang float64转decimal再加减乘除运算,解决float64数据精度问题

    package main import 34 fmt 34 34 github com shopspring decimal 34 func main x 61 decimal NewFromString 34 1 34 y 61 deci
  • Golang float64保留三位小数

    func prettyFloat64 f float64 n int float64 base 61 10 for i 61 1 i lt n i 43 43 base 61 base 10 inst 61 int64 f float64
  • Golang 计算两个时间相差多少分钟

    stime int64 etime int64 时间戳 starttime 61 time Unix stime 0 endtime 61 time Unix etime 0 costtime 61 decimal NewFromFloat
  • Spring自动配置原理

    文章目录 一 概念二 自动配置原理二 自动配置生效总结 一 概念 spring集成其他框架中 xff0c 需要编写大量的xml配置文件 xff0c 编写这些配置文件十分繁琐 xff0c 常常出行错误 xff0c 导致开发效率低 Spring
  • centos 上容器配置X11

    系统 xff1a centos 7 9 连接工具 xff1a 同一个局域网内win10电脑上安装的MobaXterm Personal 步骤 xff1a 找到对应的包 96 yum whatprovides xhost 安装yum y in
  • Java 实现线程安全的方式

    1 创建线程的三种方式 通过实现 Runnable 接口 xff1b 通过继承 Thread 类本身 xff1b 通过 Callable 和 Future 创建线程 2 线程的生命周期 新建状态 使用 new 关键字和 Thread 类或其
  • 生产者消费者问题c语言_C中的生产者消费者问题

    生产者消费者问题c语言 Here you will learn about producer consumer problem in C 在这里 xff0c 您将了解C语言中的生产者消费者问题 Producer consumer probl
  • 2.15多生产者多消费者问题

    视频链接 xff1a https www bilibili com video BV1YE411D7nH p 61 24 一 xff0c 问题描述 桌上有一只盘子 xff0c 每次只能向其中放入一个水果 爸爸向盘子中只放苹果 xff0c 妈
  • 【已解决】xterm: Xt error: Can‘t open display:

    项目场景 xff1a 在MobaXterm中 xff0c 使用Ubuntu 18 04的gdb来debug MPI并行的C 43 43 代码 问题描述 Debug时 xff0c 输入 mpiexec span class token ope
  • mariadb安装

    1 配置官方的mariadb的yum源 手动创建 mariadb repo仓库文件 touch etc yum repos d mariadb repo 然后写入如下内容 mariadb name 61 MariaDB baseurl 61
  • Java 给某段代码加超时时间

    问题原因 xff1a 使用HuTool 的DbTtil 不能设置数据库连接超时时间 xff0c 可能数据库挂了 xff0c 会导致连接一直卡在那 xff0c 也没有异常抛出 xff0c 导致线程一直占着 所以给该段代码加超时时间处理 spa
  • 使用reserve来避免不必要的内存重新分配

    STL容器的内存分配策略是 xff0c 他们会自动增长以便容纳下你放入其中的数据 xff0c 只要没有超过它的最大限制就可以 xff08 要查看最大限制可调用名为max size的成员函数 xff09 对于vector和string xff
  • zmq发布-订阅模式c++实现

    上一篇讲到zmq的安装及简单的请求 应答模式 xff0c 本篇主要来看一下zmq的pub sub代码如何实现 发布 订阅模式的特点 xff1a 1 一个发布者可以被多个订阅者订阅 xff0c 即发布者和订阅者是1 xff1a n的关系 2