zeromq学习记录(二)天气更新服务器使用ZMQ_SUB ZMQ_PUB
/**************************************************************
技术博客
http://www.cnblogs.com/itdef/
技术交流群
群号码:324164944
欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/
zeromq 指南里第二个例子是天气更新服务器
socket在代码中标记为ZMQ_SUB ZMQ_PUB
ZMQ_PUB 由发布者使用分发数据。
ZMQ_SUB 由订阅者来接受数据。需要使用setcockopt来设置订阅过滤器 否者接收不到任何内容


// wuserver_cpp.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" // // Weather update server in C++ // Binds PUB socket to tcp://*:5556 // Publishes random weather updates // // Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> // //#include <zmq.hpp> #include <stdio.h> #include <stdlib.h> #include <time.h> #if (defined (WIN32)) #include <zhelpers.hpp> #endif //#define within(num) (int) ((float) num * random () / (RAND_MAX + 1.0)) int main() { // Prepare our context and publisher zmq::context_t context(1); zmq::socket_t publisher(context, ZMQ_PUB); publisher.bind("tcp://*:5556"); //publisher.bind("ipc://weather.ipc"); // Not usable on Windows. // Initialize random number generator srandom((unsigned)time(NULL)); while (1) { int zipcode, temperature, relhumidity; // Get values that will fool the boss zipcode = within(100000); temperature = within(215) - 80; relhumidity = within(50) + 10; // Send message to all subscribers zmq::message_t message(20); snprintf((char *)message.data(), 20, "%05d %d %d", zipcode, temperature, relhumidity); publisher.send(message); } return 0; }

// wuclient_cpp.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <zmq.hpp> #include <iostream> #include <sstream> int main(int argc, char *argv[]) { zmq::context_t context(1); // Socket to talk to server std::cout << "Collecting updates from weather server…\n" << std::endl; zmq::socket_t subscriber(context, ZMQ_SUB); subscriber.connect("tcp://localhost:5556"); // Subscribe to zipcode, default is NYC, 10001 const char *filter = ""; subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen(filter)); // Process 100 updates int update_nbr; long total_temp = 0; for (update_nbr = 0; update_nbr < 100; update_nbr++) { zmq::message_t update; int zipcode, temperature, relhumidity; subscriber.recv(&update); std::istringstream iss(static_cast<char*>(update.data())); iss >> zipcode >> temperature >> relhumidity; total_temp += temperature; } std::cout << "Average temperature for zipcode '" << filter << "' was " << (int)(total_temp / update_nbr) << "F" << std::endl; getchar(); return 0; }
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话