我在 ZeroMQ 和 IPv6 方面遇到了麻烦。当我通过 IPv4 使用连接或使用“tcp://[::1]:5558”时,它会像魅力一样连接。但是,如果我使用服务器完整 IPv6 地址(在本地主机或远程主机上),它会进行连接,但不会在其他端点上获取数据。
这是我的代码示例:
client.cpp
#include <stdio.h>
#include <zmq.h>
int main(int argc, char** argv)
{
void* context = zmq_ctx_new();
void* socket = zmq_socket(context, ZMQ_SUB);
int ipv6 = 1;
zmq_setsockopt(socket, ZMQ_IPV6, &ipv6, 4);
zmq_connect(socket, "tcp://[fe80::52e5:49ff:fef8:dbc6]:5558");
//zmq_connect(socket, "tcp://[::1]:5558");
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "pub", 3);
zmq_msg_t message;
do {
zmq_msg_init (&message);
zmq_msg_recv (&message, socket, 0);
printf("%s\n", (char*)zmq_msg_data(&message));
zmq_msg_close(&message);
} while (zmq_msg_more(&message));
}
和server.cpp
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <zmq.h>
int main(int argc, char**argv)
{
void* context = zmq_ctx_new();
void* publisher = zmq_socket(context, ZMQ_PUB);
int ipv6 = 1;
zmq_setsockopt(publisher, ZMQ_IPV6, &ipv6, sizeof(int));
zmq_bind(publisher, "tcp://*:5558");
char buffer[4] = "pub";
unsigned tries = 0;
while(tries < 10) {
zmq_send(publisher, &buffer, strlen(buffer), 0);
tries++;
sleep(1);
}
return 0;
}
我正在使用 ZeroMQ 4.0.0 RC,但它也发生在 3.2 上。我在linux(slackware)上并从源代码安装了它。我还使用 jeroMQ 使用 java 服务器进行了测试,问题是相同的。我使用 REQ-REP 连接进行了另一次测试,问题是相同的。
预先感谢您的帮助。
最佳答案
fe80*
地址是本地链接,您必须指定本地主机链接名称:例如fe80...:1%eth1
fe80::/10 — Addresses in the link-local prefix are only valid and unique on a single link. Within this prefix only one subnet is allocated (54 zero bits), yielding an effective format of fe80::/64. The least significant 64 bits are usually chosen as the interface hardware address constructed in modified EUI-64 format. A link-local address is required on every IPv6-enabled interface—in other words, applications may rely on the existence of a link-local address even when there is no IPv6 routing. These addresses are comparable to the auto-configuration addresses 169.254.0.0/16 of IPv4.
关于c++ - ZeroMQ 无法通过单播 IPv6 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19012378/