如何在Linux上的C/C ++中使用ipv6 udp套接字进行多播? [英] How to multicast with ipv6 udp socket in C/C++ on linux?

查看:129
本文介绍了如何在Linux上的C/C ++中使用ipv6 udp套接字进行多播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(英语不是我的母语,不用担心某些句子是否很奇怪;)).

我正在开发 PONG游戏,并通过创建一些类来帮助我管理窗口,事件...和网络,因为我添加了 LAN功能,但目前您必须输入要与之一起玩的人的地址.解决此问题的方法是广播(扫描播放器的局域网).对于ipv4来说这很容易,只需使用地址255.255.255.255,但是我们在2017年就提供了仅适用于ipv4糟糕的功能...

然后我正在寻找一种使用ipv6进行广播的方法,并且我了解了多播,但是这部分让我迷失了方向. =(

我在Linux上的C ++中使用标准库,我发现了一些不适用于我的多播示例.我目前所做的最好的工作是将udp数据包从该程序的一个实例发送到同一台计算机上的另一个实例.

如何在C/C ++中的Linux上使用ipv6 udp套接字进行多播?

在Internet上找到的最佳代码(我对其进行了重新排列)几乎可以正常工作 (有client和serv的所有功能,可以通过在argv上加1或0来选择):

int main(int argc, char const *argv[]) {

struct sockaddr_in6 groupSock;
int sd = -1;

char databuf[10];
int datalen = sizeof databuf;

/* Create a datagram socket on which to send/receive. */
if((sd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    perror("Opening datagram socket error");
    return 1;
} else {
    cout << "Opening the datagram socket...OK." << endl;;
}

/* Enable SO_REUSEADDR to allow multiple instances of this */
/* application to receive copies of the multicast datagrams. */
int reuse = 1;
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof reuse) < 0) {
    perror("Setting SO_REUSEADDR error");
    close(sd);
    return 1;
} else {
    cout << "Setting SO_REUSEADDR...OK." << endl;
}

/* Initialize the group sockaddr structure with a */
memset((char *) &groupSock, 0, sizeof groupSock);
groupSock.sin6_family = AF_INET6;
// address of the group
inet_pton(AF_INET6, "ff0e::/16", &groupSock.sin6_addr);
groupSock.sin6_port = htons(4321);

/* Set local interface for outbound multicast datagrams. */
/* The IP address specified must be associated with a local, */
/* multicast capable interface. */
int ifindex = if_nametoindex ("enp3s0");
cout << "ifindex is " << ifindex << endl;

if(setsockopt(sd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, sizeof ifindex)) {
    perror("Setting local interface error");
    return 1;
} else {
    cout << "Setting the local interface...OK" << endl;
}

// choice is 0 for sending and 1 for receiving
int choice;
if (argc < 2) {
    cout << "missing argv[1]" << endl;
    return 1;
}
sscanf (argv[1], "%d", &choice);

// if sending
if (choice == 0) {
    memset(databuf, 'a', datalen);
    databuf[sizeof databuf - 1] = '\0';

    if (sendto(sd, databuf, datalen, 0, (sockaddr*)&groupSock, sizeof groupSock) < 0) {
        cout << "Error in send" << endl;
    } else {
        cout << "Send okay!" << endl;
    }
}

// if receiving
else if (choice == 1) {
    groupSock.sin6_addr = in6addr_any;
    if(bind(sd, (sockaddr*)&groupSock, sizeof groupSock)) {
        perror("Binding datagram socket error");
        close(sd);
        return 1;
    } else {
        cout << "Binding datagram socket...OK." << endl;
    }

    /* Join the multicast group ff0e::/16 on the local  */
    /* interface. Note that this IP_ADD_MEMBERSHIP option must be */
    /* called for each local interface over which the multicast */
    /* datagrams are to be received. */
    struct ipv6_mreq group;
    inet_pton (AF_INET6, "ff0e::", &group.ipv6mr_multiaddr.s6_addr);
    group.ipv6mr_interface = ifindex;

    if(setsockopt(sd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&group, sizeof group) < 0) {
        perror("Adding multicast group error");
        close(sd);
        return 1;
    } else {
        cout << "Adding multicast group...OK." << endl;
    }

    if (read(sd, databuf, datalen) < 0) {
        perror("Error in read");
    } else {
        databuf[sizeof databuf - 1] = '\0';// just for safety
        cout << "Read Okay" << endl;
        cout << "Message is : " << databuf << endl;
    }
}

return 0;
}

这里的地址是ff0e ::,但是我尝试使用ff01 ::和ff02 ::.

我需要帮助,但还没有找到任何简单的文档.预先感谢您的回答.

感谢Ron Maupin和Jeremy Friesner的这些评论,对我有帮助.

谢谢杰里米!您的建议是使用ff12 :: blah:blah(...)代替ff0e ::可以!我应该为我的问题写答案以关闭线程吗?

解决方案

下面的代码是正确的:
唯一错误的是用于多播的地址.
就像杰里米(Jeremy)所说的那样, ff0e ::不正确,我改用ff12 :: feed:a:dead:beef可以正常工作.

可以使用if_nameindex()获取可用接口的名称和索引.

更新:我尝试删除一些代码,看看是否没有它就可以工作,我设法做到了:

服务器:

// OPEN
int fd = socket(AF_INET6, SOCK_DGRAM, 0);

// BIND
struct sockaddr_in6 address = {AF_INET6, htons(4321)};
bind(fd, (struct sockaddr*)&address, sizeof address);

// JOIN MEMBERSHIP
struct ipv6_mreq group;
group.ipv6mr_interface = 0;
inet_pton(AF_INET6, "ff12::1234", &group.ipv6mr_multiaddr);
setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &group, sizeof group);

// READ
char buffer[128];
read(fd, buffer, sizeof buffer);

客户端:

// OPEN
int fd = socket(AF_INET6, SOCK_DGRAM, 0);

// ADDRESS
struct sockaddr_in6 address = {AF_INET6, htons(4321)};
inet_pton(AF_INET6, "ff12::1234", &address.sin6_addr);

// SEND TO
char buffer[128];
strcpy(buffer, "hello world!");
sendto(fd, buffer, sizeof buffer, 0, (struct sockaddr*)&address, sizeof address);

(English is not my native tongue, don't worry if some sentences are strange ;) ).

I was developing a PONG game and by the way creating some classes to help me managing window, event ... and network because I added a LAN feature to the game but currently you have to enter the address of the one with who you want to play with. And a solution to that was a broadcast (scanning LAN for player). This was easy with ipv4, just use the address 255.255.255.255 but we are in 2017 and provide a feature that works only with ipv4 sucks...

Then I look for a way to broadcast with ipv6 and I learn about multi-cast but this part just get me lost. =(

I use standard libraries on Linux in C++, I found several example of multi-cast that didn't work with me. The best I have done at this time is sending a udp packet from one instance of the program to an other on the same computer.

How can I multi-cast with ipv6 udp socket on Linux in C/C++ ?

The best code found on Internet (I rearranged it) that almost work (there is client and serv all in one, choice is made by adding 1 or 0 to argv) :

int main(int argc, char const *argv[]) {

struct sockaddr_in6 groupSock;
int sd = -1;

char databuf[10];
int datalen = sizeof databuf;

/* Create a datagram socket on which to send/receive. */
if((sd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    perror("Opening datagram socket error");
    return 1;
} else {
    cout << "Opening the datagram socket...OK." << endl;;
}

/* Enable SO_REUSEADDR to allow multiple instances of this */
/* application to receive copies of the multicast datagrams. */
int reuse = 1;
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof reuse) < 0) {
    perror("Setting SO_REUSEADDR error");
    close(sd);
    return 1;
} else {
    cout << "Setting SO_REUSEADDR...OK." << endl;
}

/* Initialize the group sockaddr structure with a */
memset((char *) &groupSock, 0, sizeof groupSock);
groupSock.sin6_family = AF_INET6;
// address of the group
inet_pton(AF_INET6, "ff0e::/16", &groupSock.sin6_addr);
groupSock.sin6_port = htons(4321);

/* Set local interface for outbound multicast datagrams. */
/* The IP address specified must be associated with a local, */
/* multicast capable interface. */
int ifindex = if_nametoindex ("enp3s0");
cout << "ifindex is " << ifindex << endl;

if(setsockopt(sd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, sizeof ifindex)) {
    perror("Setting local interface error");
    return 1;
} else {
    cout << "Setting the local interface...OK" << endl;
}

// choice is 0 for sending and 1 for receiving
int choice;
if (argc < 2) {
    cout << "missing argv[1]" << endl;
    return 1;
}
sscanf (argv[1], "%d", &choice);

// if sending
if (choice == 0) {
    memset(databuf, 'a', datalen);
    databuf[sizeof databuf - 1] = '\0';

    if (sendto(sd, databuf, datalen, 0, (sockaddr*)&groupSock, sizeof groupSock) < 0) {
        cout << "Error in send" << endl;
    } else {
        cout << "Send okay!" << endl;
    }
}

// if receiving
else if (choice == 1) {
    groupSock.sin6_addr = in6addr_any;
    if(bind(sd, (sockaddr*)&groupSock, sizeof groupSock)) {
        perror("Binding datagram socket error");
        close(sd);
        return 1;
    } else {
        cout << "Binding datagram socket...OK." << endl;
    }

    /* Join the multicast group ff0e::/16 on the local  */
    /* interface. Note that this IP_ADD_MEMBERSHIP option must be */
    /* called for each local interface over which the multicast */
    /* datagrams are to be received. */
    struct ipv6_mreq group;
    inet_pton (AF_INET6, "ff0e::", &group.ipv6mr_multiaddr.s6_addr);
    group.ipv6mr_interface = ifindex;

    if(setsockopt(sd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&group, sizeof group) < 0) {
        perror("Adding multicast group error");
        close(sd);
        return 1;
    } else {
        cout << "Adding multicast group...OK." << endl;
    }

    if (read(sd, databuf, datalen) < 0) {
        perror("Error in read");
    } else {
        databuf[sizeof databuf - 1] = '\0';// just for safety
        cout << "Read Okay" << endl;
        cout << "Message is : " << databuf << endl;
    }
}

return 0;
}

Here the address is ff0e:: but I have try with ff01:: and ff02::.

I need help, I have not found any simple documentation about that. Thanks in advance for any answer.

Edit : Thanks Ron Maupin and Jeremy Friesner for these comments, it helps me.

Edit : THANKS Jeremy ! Your advice to use ff12::blah:blah(...) instead of ff0e:: works ! Should I write answer to my question to close the thread ?

解决方案

This code below is right:
The only thing wrong is the address used for the multicast.
Like Jeremy said it, ff0e:: is not correct, I used instead ff12::feed:a:dead:beef and it works.

It is possible to get the name and index of the available interface by using if_nameindex().

Update : I try to remove some code to see if it work without it and I manage to get this :

server :

// OPEN
int fd = socket(AF_INET6, SOCK_DGRAM, 0);

// BIND
struct sockaddr_in6 address = {AF_INET6, htons(4321)};
bind(fd, (struct sockaddr*)&address, sizeof address);

// JOIN MEMBERSHIP
struct ipv6_mreq group;
group.ipv6mr_interface = 0;
inet_pton(AF_INET6, "ff12::1234", &group.ipv6mr_multiaddr);
setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &group, sizeof group);

// READ
char buffer[128];
read(fd, buffer, sizeof buffer);

client :

// OPEN
int fd = socket(AF_INET6, SOCK_DGRAM, 0);

// ADDRESS
struct sockaddr_in6 address = {AF_INET6, htons(4321)};
inet_pton(AF_INET6, "ff12::1234", &address.sin6_addr);

// SEND TO
char buffer[128];
strcpy(buffer, "hello world!");
sendto(fd, buffer, sizeof buffer, 0, (struct sockaddr*)&address, sizeof address);

这篇关于如何在Linux上的C/C ++中使用ipv6 udp套接字进行多播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆