(英语不是我的母语,如果有些句子很奇怪,不用担心))。如何在Linux上使用C/C++与ipv6 udp套接字进行组播?
我开发一个乒乓球比赛,顺便创造了一些课程,以帮助我管理窗口,事件......和网络因为我加了一个局域网功能的游戏,但现在你必须进入与你想玩的人的地址。对此的解决方案是广播(播放器的扫描LAN)。这很容易与ipv4,只是使用地址255.255.255.255,但我们在2017年,并提供了一个功能,只适用于ipv4吸...
然后,我寻找一种方式与ipv6广播,我了解多但这部分只是让我失去了。 =(
我在C++使用标准库在Linux上,我发现多播的几个例子没有和我一起工作。我已经在这个时候做最好的是从一个实例发送一个UDP包将该程序提供给其他在同一台计算机上。
用C我怎样才能多播IPv6的UDP套接字在Linux/C++?
在互联网上找到的最好的代码(我重新安排它)这几乎工作 (有客户端和服务器在一个,选择是由添加荷兰国际集团1或0的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;
}
这里的地址是FF0E ::但是我有FF01 ::与FF02 ::尝试。
我需要帮助,我还没有找到任何有关这方面的简单文档。预先感谢任何答案。
编辑: 感谢Ron Maupin和Jeremy Friesner对这些评论,它帮助我。
编辑: 谢谢杰里米!你的建议使用ff12 :: blah:blah(...)而不是ff0e :: works!我应该回答我的问题以关闭该线程吗?
即使IPv4的,这是广播的滥用,而且它是糟糕的网络编程。只有当您需要中断LAN上的_every_主机时才应使用广播,因为这是它的作用。IPv6消除了广播,因为它经常被滥用。在大多数情况下,您只需要中断局域网上的主机子集,这是多播。您需要注意标志和范围,并选择一个未使用的多播地址。除非确实需要中断局域网中的所有主机,否则不要使用全部节点地址。 –
根据我对IPv6-multicast-over-LAN的经验,ff12 :: blah:blah:blah(挑选你自己独特的blah值)的多播地址效果最好。请注意,多播地址是特定于接口的,因此您需要指定接口索引,并且如果您希望程序的多播在所有网络接口上工作,则需要加入多个多播组(每个网络一个接口;所有接口索引的组播地址可以相同,但需要单独加入每个网络接口并分别发送到每个网络接口) –