Why is the ZMQ_RCVHWM option of ZeroMQ ineffective?

两盒软妹~` 提交于 2019-12-12 03:47:36

问题


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:

  1. 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.

  2. But when I setup the Router, the Dealer can continue send about 4K msg and obstructed. Why?

  3. 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;
        }
    }
}

来源:https://stackoverflow.com/questions/36491006/why-is-the-zmq-rcvhwm-option-of-zeromq-ineffective

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!