有一个端点,即Dealer,以及一个端点,即Router。经销商通过TCP协议连接到路由器。我为所有人设置了ZMQ_SNDHWM
和ZMQ_RCVHWM
为1。
请注意,经销商始终发送给路由器,但路由器不会收到。
问题是:
当路由器未设置时,经销商只发送一条消息然后被阻塞。这是对的,因为ZMQ_SNDHWM
是1。
但是当我设置路由器时,经销商可以继续发送大约4K消息并阻塞。为什么呢?
另外,如果我让路由器只接收一条消息,经销商可以再次继续发送大约4K消息,但即使路由器继续接收它也不能再发送。看来经销商已经死了。为什么会这样?
经销商代码:
// 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);
}
路由器代码:
// 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;
}
}
}