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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值