扯淡的多播参数IP_MULTICAST_LOOP

因为本文是用JAVA做的测试,JAVA在这个地方有一点奇怪。接口名称与实现(即文档名称是冲突),看JDK源码:

当MulticastSocket的loopbackMode为true时,表示禁用IP_MULTICAST_LOOP。这一点与常规思路有点不同。

way is ugly, about socket.setLoopbackMode at least. When I see this line, I really think it is to enable socket’s LoopbackMode if no the following comment, but actually the name of the parameter needed by setLoopbackMode is "disable.先上多播通信的模拟源代码,接收端的:

接收端:

在Windows平台:分别启动服务端,香港服务器,客户端在同一台Windows机器上:java -cp . com.guojje.mcast.MultiServer -l falsejava -cp . com.guojje.mcast. McastClient -p 7800 –l false发现服务端输出收到包: receive(from /192.168.1.62:7800):hello!!receive(from /192.168.1.62:7800):hello!!receive(from /192.168.1.62:7800):hello!!receive(from /192.168.1.62:7800):hello!!说明多播通信是正常的。下一步我们把服务端启动脚本改为java -cp . com.guojje.mcast.MultiServer -l true 即禁用IP_MULTICAST_LOOP,发现服务端无法收到数据包。我们再测试客户:java -cp . com.guojje.mcast. McastClient -p 7800 –l true.发现对于发送端来说,IP_MULTICAST_LOOP为true 或false对结果没有影响。那么linux是否也如此呢。我们首先清空linux的IPV6环境,查看IPV6模块是否装载:[root@localhost ~]# lsmod | grep ipv6ipv6 251137 18发现存在IPV6模块,则关闭IPV6:使用vi编辑器,打开/etc/modprobe.conf .在文档中加入如下的两条:alias net-pf-10 offalias ipv6 off重启机器。查看多播是否启动:[root@localhost javawork]# route -eKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.1.0 * 255.255.255.0 U 0 0 0 eth0169.254.0.0 * 255.255.0.0 U 0 0 0 eth0default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0发现没有多播路由,增加多播路由:[root@localhost javawork]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0难证一下:[root@localhost javawork]# route -eKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.1.0 * 255.255.255.0 U 0 0 0 eth0169.254.0.0 * 255.255.0.0 U 0 0 0 eth0224.0.0.0 * 240.0.0.0 U 0 0 0 eth0default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0[root@localhost ~]# netstat -gnIPv6/IPv4 Group MembershipsInterface RefCnt Group————— —— ———————lo 1 224.0.0.1eth0 1 224.0.0.251eth0 1 224.0.0.1OK,开始测试。分别启动服务端,客户端在同一台linux相器上:java -cp . com.guojje.mcast.MultiServer –b 234.2.3.4java -cp . com.guojje.mcast. McastClient -p 7800这里要注意与Windows系统有不同,网站空间,MulticastSocket Server端绑定的地址必须是多播或者不指定,否则收不到多播包。参考我的另一篇文章:这里出现另一个奇怪的现象,对于服务端java -cp . com.guojje.mcast.MultiServer –b 234.2.3.4 -l true / falseIP_MULTICAST_LOOP为true 或false对结果没有影响。但客户端出怪事了。java -cp . com.guojje.mcast.McastClient -b 192.168.1.90 -l true当IP_MULTICAST_LOOP为true,虚拟主机,即禁用loopbackmode时,服务端无法收到多播包.从msdn上找到这样一段话(Sun的bug database也有相应的引用):

忍耐力较诸脑力,尤胜一筹。

扯淡的多播参数IP_MULTICAST_LOOP

相关文章:

你感兴趣的文章:

标签云: