我的母语不是英语,如果有些句子有点奇怪,请不要担心;)。
我正在开发一个 PONG游戏 ,同时创建了一些类来帮助我管理窗口、事件等,还添加了一个 网络 功能,因为我在游戏中增加了一个 局域网功能,但目前你必须输入想要玩的人的地址。解决这个问题的方法是使用 广播(扫描局域网查找玩家)。这对于ipv4来说很容易,只需要使用地址255.255.255.255,但现在已经是2017年了,只提供ipv4功能的特性太过时了……
然后我寻找了一种用ipv6进行广播的方法,我了解到了多播,但这部分让我迷失了。=(
我使用了 C++ Linux标准库,找到了几个多播示例,但它们对我没有用。到目前为止,我做得最好的事情是从程序的一个实例向同一台计算机上的另一个实例发送udp数据包。
如何在Linux上使用C/C++ ipv6 udp socket进行多播?
我在互联网上找到的最好的代码(重新排列了一下),几乎可以工作(客户端和服务器都在同一个程序中,通过给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的评论,它们对我很有帮助。
编辑: 谢谢Jeremy!您建议使用ff12::blah:blah(...)而不是ff0e::,这个方法有效!我应该写答案来关闭主题吗?