【发布时间】:2017-09-22 09:24:07
【问题描述】:
我正在使用 ØMQ 进行锻炼,并参考 zguide 使用 cppzmq 开发了一个简单的发布者/订阅者。请找到与下面相同的代码 sn-p,我能够成功发布和订阅数据和内存占用也相当可观。由于这是分布式消息传递系统,我想在我的项目中使用它。
publisher.cpp
int main (int argc, char * argv[]) {
// Prepare our context and publisher
zmq::context_t context(1);
zmq::socket_t publisher(context, ZMQ_PUB);
std::string addr("tcp://*:");
addr += ((argc > 1) ? std::string(argv[1]) : "5563");
std::string tappend((argc > 2) ? std::string(argv[2]) : "");
publisher.bind(addr.c_str());
struct timeval timeofday;
for(unsigned int idx = 0; idx < Total_Topics; ++idx)
{
Topics.emplace(std::make_pair(idx, std::string("temperature/celsius" + tappend + std::to_string(idx))));
}
std::this_thread::sleep_for (std::chrono::seconds(2));
for(unsigned int NoOfTimes = 0; NoOfTimes < 20000; ++NoOfTimes)
{
for(unsigned int count = 0; count < Topics.size(); ++count)
{
{
Quote quote = {};
quote.ticker = "BHELL";
gettimeofday(&timeofday,NULL);
quote.timestampus = timeofday.tv_usec;
quote.timestamps = timeofday.tv_sec;
std::stringstream ss;
quote.value *= 0.1;
quote.data = std::to_string(NoOfTimes) + std::to_string(count);
ss << quote;
s_sendmore(publisher, Topics[count]);
std::cout << "Publish "<< Topics[count] << std::endl;
s_send(publisher, ss.str());
}
}
std::this_thread::sleep_for (std::chrono::seconds(8));
}
return 0;
}
subscriber.cpp
int main (int argc, char * argv[]) {
// Prepare our context and subscriber
zmq::context_t context(1);
zmq::socket_t subscriber (context, ZMQ_SUB);
std::string addr("tcp://localhost:");
addr += ((argc > 1) ? std::string(argv[1]) : "5563");
std::string tappend((argc > 2) ? std::string(argv[2]) : "");
subscriber.connect(addr.c_str());
struct timeval timeofday;
std::string topic("");
for(unsigned int idx = 0; idx < Total_Topics; ++idx)
{
topic = std::string("temperature/celsius" + tappend + std::to_string(idx));
subscriber.setsockopt( ZMQ_SUBSCRIBE, topic.c_str(), topic.length());
}
while(1)
{
{
Quote quote;
std::stringstream ss;
std::string address = s_recv (subscriber);
std::cout<<"Msg Rcvd" << std::endl;
std::cout<<"Topic: " << address << std::endl;
std::string contents = s_recv (subscriber);
ss << contents;
ss >> quote;
gettimeofday(&timeofday,NULL);
std::cout << "us diff " << (timeofday.tv_usec - quote.timestampus) << std::endl;
std::cout << "Sec diff " << (timeofday.tv_sec - quote.timestamps) << std::endl;
}
}
return 0;
}
现在我想知道 zeroMQ 应该使用哪种安全技术。我知道有curveZMQ 和czmq。但我不确定我应该使用哪一个。任何人都已经使用过这些,或者您有任何其他用于 ZeroMQ 安全性的库。请指教。
【问题讨论】:
-
CurveZMQ 是你想要的。 czmq 为基础 ZeroMQ 库(用 C++ 编写)提供了一个 C 接口。它不提供加密。
-
@colini,感谢您对此有所了解。我对 zeroMQ 还很陌生。我现在完全明白了。 CurveZMQ 有 node.js 绑定吗?谢谢
标签: c++ encryption zeromq