zeromq cppzmq

创建日期: 2024-06-11 17:42 | 作者: 风波 | 浏览次数: 14 | 分类: C++

项目地址:https://github.com/zeromq/cppzmq 文档地址:https://brettviren.github.io/cppzmq-tour/index.html

这个库依赖 libzmq,使用 c++ 风格把 libzmq 的 API 封装了一下。

#include <zmq.hpp>

int main()
{
    zmq::context_t ctx;
    zmq::socket_t sock(ctx, zmq::socket_type::push);
    sock.bind("inproc://test");
    sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
}

官方文档:https://zguide.zeromq.org/docs/chapter1/

#include <zmq.hpp>
#include <string>
#include <iostream>
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>

#define sleep(n)    Sleep(n)
#endif

int main () {
    //  Prepare our context and socket
    zmq::context_t context (2);
    zmq::socket_t socket (context, zmq::socket_type::rep);
    socket.bind ("tcp://*:5555");

    while (true) {
        zmq::message_t request;

        //  Wait for next request from client
        socket.recv (request, zmq::recv_flags::none);
        std::cout << "Received Hello" << std::endl;

        //  Do some 'work'
        sleep(1);

        //  Send reply back to client
        zmq::message_t reply (5);
        memcpy (reply.data (), "World", 5);
        socket.send (reply, zmq::send_flags::none);
    }
    return 0;
}
14 浏览
9 爬虫
0 评论