Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

c++ - ZMQ poll not working

When I run the following code, I get an error on the first call to zmq_poll (i.e. it returns -1). The zmq_errno() returns 128 and the zmr_strerror(128) call returns "Unknown error". I have been using ZMQ with C++ for a while now without any problems, but I can't get a call to zmq_poll to work, no matter how simple it is.

Calling zmq::version reveals that I am using ZMQ version 2.1.10.

Does anyone have an idea as to why zmq_poll is failing?

#include <zmq/zmq.hpp>

int main(int argc, char* argv[])
{
    zmq::context_t context(1);
    zmq::socket_t repA(context, ZMQ_REP);
    zmq::socket_t repB(context, ZMQ_REP);
    repA.bind("tcp://127.0.0.1:5555");
    repB.bind("tcp://127.0.0.1:5556");
    zmq::pollitem_t items[] =
    {
        { &repA, 0, ZMQ_POLLIN, 0 },
        { &repB, 0, ZMQ_POLLIN, 0 }
    };
    while (true)
    {
        int rc = zmq_poll(items, 2, 1000);
        if (rc < 0)
        {
            int code = zmq_errno(); //code = 128
            auto message = zmq_strerror(code); //message = "Unknown error"
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To obtain a ?MQ socket for use in a zmq_pollitem_t structure, you should cast an instance of the socket_t class to (void *).

So it should be

zmq::pollitem_t items[] =
{
    { repA, 0, ZMQ_POLLIN, 0 },
    { repB, 0, ZMQ_POLLIN, 0 }
};

Without the &.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...