With Zeromq, you use a string to bind or connect. It starts with protecol, tcp:// in your case, it's all right. Then you have a '*' which is for all available devices. Then you end with the port number :0 in your case.
socket.bind("tcp://*:2424)
Let's try to bind on port 2424. If you run man zmq_tcp, it will report a port number above 1024. In principle, you should know your port number in advance, and not after binding. In newer versions 3.2, you can also specify the port: 0 or: *, then os will decide where the port is. This can be obtained using socket.getsockopt (), as shown in the following example:
zmq::context_t context(1); zmq::socket_t sock(context, ZMQ_REP); char port[1024]; //make this sufficiently large. //otherwise an error will be thrown because of invalid argument. size_t size = sizeof(port); try{ sock.bind("tcp://*:*"); } catch (zmq::error_t&e ){ cerr << "couldn't bind to socket: " << e.what(); return e.num(); } sock.getsockopt( ZMQ_LAST_ENDPOINT, &port, &size ); cout << "socket is bound at port " << port << endl;
This will produce the following result, for example:
socket is bound at port tcp:
So, you still have to parse 53269 from the line "tcp: //0.0.0.0: 53269" hope this helps
source share