Java之MulticastSocket绑定到多播地址

在linux上机器运行netstat -ano,发现这种类型的条目:
udp 238.6.7.8:8012 0.0.0.0:* off/java
一般多播地址都是做为一个目的地址,为什么吗会显示在这儿呢。
经调查发现: MulticastSocket可以绑定到多播地址, 这样这个MulticastSocket
作为receive Socket效果就一目了然了:

MulticastSocket s = new MulticastSocket(new InetSocketAddress("238.6.7.8",8012)).

目前仅linux,Solaris,hp能支持该功能,在windows平台还不能绑定的多播地址.

另外一个发现在linux平台上只能绑定到多播地址或不指定,否则将无法收到多播包,代码如上所示.

而Windows平台只能绑定到监听接口的地址上或不指定.

MulticastSocket s = new MulticastSocket(new InetSocketAddress("192.168.1.105",8012)).

具体原因未明,可能是JVM的BUG,不过没找到BUG Report!

最近在一篇文章找到了答案:

文章地址:http://www.dest-unreach.org/socat/doc/socat-multicast.html

Do not bind()

When using multicast communications, you should not bind the sockets to a specific IP address. It seems that the (Linux) IP stack compares the destination address with the bind address, not taking care of the multicast property of the incoming packet.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章