问题
I'm using ZeroMQ with NACK-Oriented Reliable Multicast ( NORM ) norm://
protocol. The documentation contains only a Python code, so here is my C++ code:
PUB
Sender :
string sendHost = "norm://2,127.0.0.1:5556";// <NormNodeId>,<addr:port>
string tag = "MyTag";
string sentMessage = "HelloWorld";
string fullMessage = tag + sentMessage;
zmq::context_t *context = new zmq::context_t( 20 );
zmq::socket_t publisher( *context, ZMQ_PUB );
zmq_connect( publisher, sendHost.c_str() );
zmq_send( publisher,
fullMessage.c_str(),
fullMessage.size(),
0
);
SUB
Receiver :
char message[256];
string receiveHost = "norm://1,127.0.0.1:5556";// <NormNodeId>,<addr:port>
string tag = "MyTag";
zmq::context_t *context = new zmq::context_t( 20 );
zmq::socket_t subscriber( *context, ZMQ_SUB );
zmq_bind( subscriber, receiveHost.c_str() );
zmq_setsockopt( subscriber, ZMQ_SUBSCRIBE, tag.c_str(), tag.size() );
zmq_recv( subscriber,
message,
256,
0
);
cout << bytesReceived << endl;
cout << message << endl;
The problem I'm facing is that according to the documentation both .bind()
and .connect()
are interchangeable.
In my case they both do a .bind()
, which causes ZeroMQ to throw an error saying the second bind fails, due to address already in use error.
回答1:
... they both do a bind, which causes ZeroMQ to throw an error saying the second bind fails
Yes, this is a correct state to fail.
The first .bind()
"takes ownership" of the port and this is an exclusive role.
The interchangeability of { .bind() | .connect() }
is to be understood so that it does not matter which side .bind()
-s and which one .connect()
-s.
Until this moment, I saw no one interpreting this property in such a manner, that both sides would try to .connect()
( a non-existent .bind()
-(not)-exposed Access Point ), the less to try to .bind()
an already "occupied" port ( in case of residing on the same localhost ), or to remain in a nox-et-solitudo state, for the cases that either of the .bind()
-s establishes such a .connect()
-ready state on both ports on different localhost-s, which both after that remain in a silent solitude ( forever ), as there is ( and will be ) no attempt to make any .connect()
-ion going live and operational.
No, you need just 1
.bind()
, that may since that moment handle 0+
future .connect()
-requests, arriving to establish a live-channel PUB/SUB
, for any respective <transport-class>
protocol, including the newly added norm://
.
Anyways, welcome norm://
to the Family of ZeroMQ protocols.
Confused ?
May enjoy a further 5-seconds read
about the main conceptual differences in [ ZeroMQ hierarchy in less than a five seconds ] or other posts and discussions here.
来源:https://stackoverflow.com/questions/50753588/zeromq-with-norm-address-already-in-use-error-was-thrown-on-2nd-bind-why