大佬教程收集整理的这篇文章主要介绍了如何在Linux上的C/C++中使用ipv6 udp socket进行组播?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个PONG游戏,顺便创建一些类来帮助我管理窗口,事件……和网络因为我在游戏中添加了局域网功能,但是目前你必须输入你想要的人的地址玩.并且解决方案是广播(用于播放器的扫描LAN).这很容易使用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,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,(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,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,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,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 ::有效!我应该写我的问题的答案来关闭线程吗?
可以使用if_nameindex()获取可用接口的名称和索引.
更新:我尝试删除一些代码,看看它是否可以正常工作,我设法得到这个:
服务器:
// OPEN int fd = socket(AF_INET6,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,&group,sizeof group); // READ char buffer[128]; read(fd,buffer,sizeof buffer);
客户:
// OPEN int fd = socket(AF_INET6,0); // ADDRESS struct sockaddr_in6 address = {AF_INET6,htons(4321)}; inet_pton(AF_INET6,&address.sin6_addr); // SEND TO char buffer[128]; strcpy(buffer,"Hello World!"); sendto(fd,sizeof buffer,sizeof address);
以上是大佬教程为你收集整理的如何在Linux上的C/C++中使用ipv6 udp socket进行组播?全部内容,希望文章能够帮你解决如何在Linux上的C/C++中使用ipv6 udp socket进行组播?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。