java - Join multicast group using DatagramChannel without specifying network interface -
when using java's multicast socket can join multicast group without specifying networkinterface using code:
multicastsocket sock = new multicastsocket(port); sock.joingroup(addr);   if want use nio on other hand can do:
datagramchannel dc = datagramchannel.open(standardprotocolfamily.inet)         .setoption(standardsocketoptions.so_reuseaddr, true)         .bind(new inetsocketaddress(port))         .setoption(standardsocketoptions.ip_multicast_if, ifc);  dc.join(addr, ifc);   where ifc networkinterface interested on.  if dont know network interface in advance how can join group multicastsocket?
one solution found using code:
multicastsocket msock = new multicastsocket(); networkinterface ifc = msock.getnetworkinterface(); msock.close(); datagramchannel dc = datagramchannel.open(standardprotocolfamily.inet)         .setoption(standardsocketoptions.so_reuseaddr, true)         .bind(new inetsocketaddress(port))         .setoption(standardsocketoptions.ip_multicast_if, ifc);  dc.join(addr, ifc);   surprisingly code works , performed expected, when looking on networkinterface returned multicastsocket.getnetworkinterface() method saw returned interface named "0.0.0.0" of course not exists. there no way network interface  of networkinterface.* factories
is solution reliable? can explain why works , if there better way achieve wants?
i using local address can find lan devices! can try it! e.g   networkinterface ifc = networkinterface.getbyinetaddress(inetaddress.getlocalhost()); 
Comments
Post a Comment