继 <ZMQ的学习和研究> 学习代码 Multiple socket reader in C++
2016-04-15 14:03
721 查看
Multiple socket reader in C++
//
//Reading from multiple sockets in C++
//This version uses a simple recv loop
//
// Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com>
#include "zhelpers.hpp"
int main (int argc,char
*argv[])
{
//Prepare our context and sockets
zmq::context_t context(1);
//Connect to task ventilator
zmq::socket_t receiver(context, ZMQ_PULL);
receiver.connect("tcp://localhost:5557");
//Connect to weather server
zmq::socket_t subscriber(context, ZMQ_SUB);
subscriber.connect("tcp://localhost:5556");
subscriber.setsockopt(ZMQ_SUBSCRIBE,"10001 ",
6);
//Process messages from both sockets
//We prioritize traffic from the task ventilator
while (1) {
//Process any waiting tasks
bool rc;
do {
zmq::message_t task;
if ((rc= receiver.recv(&task, ZMQ_DONTWAIT))==
true) {
//process task
}
}
while(rc ==
true);
//Process any waiting weather updates
do {
zmq::message_t update;
if ((rc= subscriber.recv(&update, ZMQ_DONTWAIT))==
true) {
//process weather update
}
}
while(rc ==
true);
//No activity, so sleep for 1 msec
s_sleep(1);
}
return0;
}
相关文章推荐
- c语言词法分析器实验程序(c语言版)
- c++ 类的初步总结
- STL之sort快排小结
- C++类内存布局图(成员函数和成员变量分开讨论)
- STL之qsort快排小结
- 高速排序C++实现
- C++ 串
- c++虚函数实现
- 复杂链表的复制
- C语言 gets() 和 scanf() 函数的区别
- c/c++ 内存分配
- C语言-郝斌笔记-007是否为素数
- C语言-郝斌笔记-006排序及查找
- C++ 学习之路
- C++ string 跨平台 字符串分割函数
- 划分树
- C/C++中的const int*和int * const
- C语言-郝斌笔记-005菲波拉契序列
- gdb调试 list显示“in **.cpp”
- C语言-郝斌笔记-004判断是否为回文数