2

我在使用 ZeroMQ 和 IPv6 时遇到了麻烦。当我使用通过 IPv4 的连接或使用“tcp://[::1]:5558”时,它的连接就像一个魅力。但是,如果我使用服务器完整的 IPv6 地址(在我的本地主机或远程主机上)它会连接,但不会在另一个端点上获取数据。

这是我的代码示例:

客户端.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 连接做了另一个测试,问题是一样的。

提前感谢您的帮助。

4

1 回答 1

4

fe80* addresses are link local, you must specify the local hosts link name: e.g. 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.

http://en.wikipedia.org/wiki/IPv6_address#Local_addresses

于 2013-09-25T19:37:36.530 回答