[zeromq-dev] Publisher SND_HWM
Mathieu Westphal
mathieu.westphal at gmail.com
Fri Oct 17 17:49:55 CEST 2014
Hello
In the following example :
server :
#include <zmq.hpp>
#include <iostream>
int main () {
// Prepare our context and publisher
zmq::context_t context (1);
zmq::socket_t publisher (context, ZMQ_PUB);
// Set high water mark to 1 NOT WORKING ?
int hwm = 1;
publisher.setsockopt(ZMQ_SNDHWM, &hwm, sizeof(hwm) );
publisher.bind("tcp://*:5556");
// Server main loop
int counter = 0;
while (1) {
counter++;
// Send message to all subscribers
zmq::message_t z_counter( sizeof(counter) );
memcpy( z_counter.data(), &counter, sizeof(counter) );
publisher.send( z_counter );
std::cout << counter << std::endl;
}
return 0;
}
client
#include <unistd.h>
#include <iostream>
#include <zmq.hpp>
int main(int argc, char* argv[]) {
// Creating zmq context
zmq::context_t context (1);
zmq::socket_t subscriber (context, ZMQ_SUB);
subscriber.connect("tcp://localhost:5556");
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
// main client loop
int counter;
zmq::message_t update;
while (true) {
// Subscribing/Receiving/Unsibscribing in order to limit the amount of queued messages.
subscriber.recv(&update);
std::cout << *(static_cast<int*>(update.data())) << std::endl;
usleep(1000000);
}
return 0;
}
if i
launch the client
launch the server and after 3 seconds stop it
the client still receive hundreds of messages ( like 0.2% of it ) , wich have huge delay ( the server being already dead ).
I would have exepcted to receive only three message( one by seconds ), considering the fact that i've set the publisher high water mark to 1.
The HWM has an effect , because if i remove it i receive like all the messages, but i do not understand where all these message are stored .
I know i cas une ZMQ_CONFLATE to obtain the correct effect, i will probably use multiparts message in the future.
Mathieu
More information about the zeromq-dev
mailing list