'cppzmq fails to receive tcp messages
I am trying to use ZMQ socket on my Ubuntu machine to communicate with a ESP8266 edge device. I tried this piece of Python code which works fine:
import zmq
ctx = zmq.Context()
router = ctx.socket(zmq.ROUTER)
router.router_raw = True
router.bind("tcp://*:8081")
while True:
msg = router.recv_multipart()
identity, body = msg
print(identity)
print(body)
as it gives (server side)
b'\x00k\x8bEg'
b''
b'\x00k\x8bEg'
b'hello from ESP8266'
b'\x00k\x8bEg'
b'\r\n'
but when I translate it into C++ as
#include <zmq_addon.hpp>
int main () {
zmq::context_t context;
zmq::socket_t socket(context, zmq::socket_type::router);
int router_raw = 1;
zmq_setsockopt(&socket, ZMQ_ROUTER_RAW, &router_raw, 1);
socket.bind("tcp://*:8081");
while (true) {
std::cout << "listening " << std::endl;
std::vector<zmq::message_t> msgs;
if (zmq::recv_multipart(socket, std::back_inserter(msgs))) {
std::cout << "got " << static_cast<const char *> (msgs.front().data())
<< std::endl;
}
}
return 0;
}
it doesn't work any more and hangs before recv_multipart, though at the same time ESP8266 client do recieve some wierd ⸮ symbol which indicates tcp connection success I guess.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
