当前位置: 首页 > news >正文

zeromq的使用(windows)

注:本文说的是windows平台的使用

1.官网下载源码:http://zeromq.org/intro:get-the-software

下载stable release is v4.1.6

 2.vs2013进行编译lib库和dll(只编译libzmq工程即可)

找到路径:zeromq-4.1.6\builds\msvc\vs2013,用vs2013打开libzmq.sln。

直接编译libzmq会报错:

LINK : fatal error LNK1104: cannot open file 'libsodium.lib'

将libsodium设置为不链接:

 编译成功,编译后的文件(以win32 Releasewei)在zeromq-4.1.6\bin\Win32\Release\v120\dynamic下:

3.实战

将lib文件和头文件zmq.h、zmq_utils.h引入工程

(1)发布

// pub.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include"zmq.h"
#include"zmq_utils.h"

int main() {
	void *context = zmq_ctx_new();
	void *publisher = zmq_socket(context, ZMQ_PUB);
	zmq_bind(publisher, "tcp://*:9998");
	char sendbuf[64] = {0};
	int cnt = 0;
	while (cnt < 1000)
	{
		sprintf_s(sendbuf, "msg消息----%d", cnt++);
		std::cout << "msg消息----" << cnt << std::endl;
		zmq_send(publisher, sendbuf, strlen(sendbuf), 0);
		Sleep(1000);
		
	}
	std::cout << "publisher over!" << std::endl;
	zmq_close(publisher);
	zmq_ctx_destroy(context);
	getchar();
	return 0;
}

(2)订阅

// sub.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include"zmq.h"
#include"zmq_utils.h"

int main(void)
{
	void *context = zmq_ctx_new();
	void *subscriber = zmq_socket(context, ZMQ_SUB);

	zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", 0);
	zmq_connect(subscriber, "tcp://127.0.0.1:9998");
	while(1)
	{
		char recvbuf[64] = {0};
		int ret = zmq_recv(subscriber, recvbuf, 64, ZMQ_DONTWAIT);//ZMQ_DONTWAIT表示zmq_recv设置为非阻塞
		if (ret == -1)
		{
			if (errno == EAGAIN) continue;
			else
			{
				std::cout << "接收错误"<< std::endl;
				break;
			}
		}
			std::cout << recvbuf << std::endl;
	}
	zmq_close(subscriber);
	zmq_ctx_destroy(context);
	getchar();
	return 0;
}

 

 

API:https://www.cnblogs.com/fengbohello/p/4230135.html

相关文章:

  • DLL的种类
  • ASSERT(AfxGetThread() == NULL);
  • WebRTC
  • 剑指2题目汇总(一)
  • 剑指2题目汇总(二)
  • 剑指2题目汇总(三)
  • 对话框屏蔽Esc键的方法
  • 当你写的程序CPU利用率飙升到50%以上怎么办?
  • 多线程中的count++问题---互斥、自旋、原子操作
  • MySQL集群
  • MongoDB远程端口不通
  • C++(Linux)操作MongoDB
  • MySQL外网连接不上
  • IP首部、TCP首部
  • ZeroMQ为什么高效?
  • 〔开发系列〕一次关于小程序开发的深度总结
  • CentOS从零开始部署Nodejs项目
  • CSS实用技巧
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • Vue2 SSR 的优化之旅
  • windows下mongoDB的环境配置
  • 成为一名优秀的Developer的书单
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 力扣(LeetCode)21
  • 面试总结JavaScript篇
  • 前嗅ForeSpider教程:创建模板
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • linux 淘宝开源监控工具tsar
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #图像处理
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (3)(3.5) 遥测无线电区域条例
  • (day 12)JavaScript学习笔记(数组3)
  • (k8s中)docker netty OOM问题记录
  • (LeetCode C++)盛最多水的容器
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (ZT)薛涌:谈贫说富
  • (多级缓存)多级缓存
  • (分布式缓存)Redis持久化
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (实战篇)如何缓存数据
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (转)大型网站的系统架构
  • .gitignore文件---让git自动忽略指定文件
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .Net Core缓存组件(MemoryCache)源码解析
  • .net framework profiles /.net framework 配置
  • .NET MVC第三章、三种传值方式