在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.