There is an endpoint, which is Dealer, and an endpoint which is Router. The Dealer is connected to the Router by TCP protocol. I set ZMQ_SNDHWM
and ZMQ_RCVHWM
to 1 for all of them.
Note that the Dealer always sends to Router, but the Router does not receive.
The questions are:
When the Router is not set up, the Dealer just sends one message and is then obstructed. That is right, because
ZMQ_SNDHWM
is 1.But when I setup the Router, the Dealer can continue send about 4K msg and obstructed. Why?
Another, if I let Router receive just one message, the Dealer can continue send about 4K msg again, but it can`t send any more even though the Router continues to receive. It seems the Dealer is dead. Why did this happen?
The Dealer code:
// create ctx void* ctx = zmq_ctx_new(); assert(nullptr != ctx); // create in void* in = zmq_socket(ctx, ZMQ_DEALER); assert(in); int sndhwm = 1; assert(0 == zmq_setsockopt(in, ZMQ_SNDHWM, &sndhwm, sizeof(sndhwm))); assert(0 == zmq_setsockopt(in, ZMQ_RCVHWM, &sndhwm, sizeof(sndhwm))); int rc = zmq_connect(in, "tcp://127.0.0.1:1012"); assert(!rc); char content[100] = {0}; int size = 0; int64_t nCount = 0; while(1) { sprintf_s(content, "%d", ++nCount); size = strlen(content); rc = zmq_send(in, content, size, 0); assert(rc = size); printf("in = %d\n", nCount); }
The Router code:
// create ctx void* ctx = zmq_ctx_new(); void* out = zmq_socket(ctx, ZMQ_ROUTER); int sndhwm = 1; assert(0 == zmq_setsockopt(out, ZMQ_SNDHWM, &sndhwm, sizeof(sndhwm))); assert(0 == zmq_setsockopt(out, ZMQ_RCVHWM, &sndhwm, sizeof(sndhwm))); int rc = zmq_bind(out, "tcp://127.0.0.1:1012"); assert(!rc); while(1) { string cmd; cin >> cmd; zmq_msg_t msg; while(true) { zmq_msg_init(&msg); rc = zmq_recvmsg(out, &msg, 0); assert(rc > 0); printf("out = %s\n", (char*)zmq_msg_data(&msg)); if(!zmq_msg_more(&msg)) { break; } } }