/**************************************************************
技术博客
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();
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 () { int zipcode, temperature, relhumidity; // Get values that will fool the boss
zipcode = within();
temperature = within() - ;
relhumidity = within() + ; // Send message to all subscribers
zmq::message_t message();
snprintf((char *)message.data(), ,
"%05d %d %d", zipcode, temperature, relhumidity);
publisher.send(message); }
return ;
}
// wuclient_cpp.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <zmq.hpp>
#include <iostream>
#include <sstream> int main(int argc, char *argv[])
{
zmq::context_t context(); // 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 = ;
for (update_nbr = ; update_nbr < ; 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 ;
}

zeromq学习记录(二)天气更新服务器使用ZMQ_SUB ZMQ_PUB的更多相关文章

  1. JavaScript学习记录二

    title: JavaScript学习记录二 toc: true date: 2018-09-13 10:14:53 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  2. Material Calendar View 学习记录(二)

    Material Calendar View 学习记录(二) github link: material-calendarview; 在学习记录一中简单翻译了该开源项目的README.md文档.接下来 ...

  3. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  4. zeromq学习记录(四)使用ZMQ_ROUTER ZMQ_DEALER

    /************************************************************** 技术博客 http://www.cnblogs.com/itdef/   ...

  5. OpenFlow1.3.3 学习记录(持续更新)

    OpenFlow1.3.3 学习记录(持续更新) 正在学习OpenFlow1.3,该篇笔记将日常更新,主要内容大致为官方文档的总结与翻译. 交换机组件 按照优先级顺序进行包匹配,如果匹配到流表项,则执 ...

  6. 2.VUE前端框架学习记录二

    VUE前端框架学习记录二:Vue核心基础2(完结)文字信息没办法描述清楚,主要看编码实战里面,有附带有一个完整可用的Html页面,有需要的同学到脑图里面自取.脑图地址http://naotu.baid ...

  7. zeromq学习记录(一)最初的简单示例使用ZMQ_REQ ZMQ_REP

    阅读zeromq guide的一些学习记录 zeromq官方例子 在VC下运行会有些跨平台的错误 我这里有做修改 稍后会发布出来 相关的代码与库  http://download.zeromq.org ...

  8. JavaWeb学习(二)———Tomcat服务器学习和使用(一)

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  9. oracle数据库学习记录(持续更新中...)

    --------------------------------------------day1------------------------------------------------- 1. ...

随机推荐

  1. Hive 简介

    hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成本低,可以通过 ...

  2. JAVA Serverlet 请求头信息和响应头信息

    <1>获取请求头信息 //获取请求头信息的全部名称 Enumeration<String> header = request.getHeaderNames(); while(h ...

  3. 转载:MySQL和Redis 数据同步解决方案整理

    from: http://blog.csdn.net/langzi7758521/article/details/52611910 最近在做一个Redis箱格信息数据同步到数据库Mysql的功能. 自 ...

  4. springboot+jsp 遇到的坑

    springboot  使用jsp: 1,修改配置文件, spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp 2,pom 加入: <dep ...

  5. 机房servlet过滤器

    1.源代码 loginform.html <html> <head> <title>使用过滤器改变请求编码</title> <meta http- ...

  6. do{}while() ;异常语句

    //while (true) //只要括号里面是true(正确的如:(1==1)),就会无限循环 //{ //} //do{}while() //不管while满足与否,首先先做一遍 //然后去看wh ...

  7. 122. Best Time to Buy and Sell Stock II (Array;Greedy)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. phpStudy4——前端页面使用Ajax请求并解析php返回的json数据

    项目需求: 在html页面显示所有用户列表信息. 需求分析: 1. html页面使用ajax向后端php请求用户数据 2. php脚本查询数据库,并将查询后的结果以json格式返回前端html页面 3 ...

  9. Python调shell

    os.system(cmd) 函数返回cmd的结束状态码,阻塞调用. os.popen(cmd) 函数返回cmd的标准输出,阻塞调用. (status, output) = commands.gets ...

  10. c++流操作

    非缓冲标准出错流对象cerr和缓冲标准出错流对象clog,它们都是来自于ostream类的对象,用于输出错信息.cerr和clog之间的不同之处在于cerr是不经过缓冲区直接向显示器输出有关信息,而c ...