java - Netty 4 and 5 - multicast not working for me -
i have been trying udp multicast working netty 4.0.26 , 5.0.0.alpha2, without success. have adapted code this post in edited form supposedly works, not me. attached code echoes "send 1", "send 2" etc. corresponding packets never received.
in expression localsocketaddr = new inetsocketaddress(localaddr, mcast_port)
have tried port 0 well, without success. kinds of other combinations of bind port , local address have been tried also.
the various socket options copied other post mentioned.
can tell me i'm doing wrong? java 8 on windows 8.1.
thanks much,
sean
import io.netty.bootstrap.bootstrap; import io.netty.buffer.bytebuf; import io.netty.buffer.unpooled; import io.netty.channel.channelfactory; import io.netty.channel.channelhandlercontext; import io.netty.channel.channeloption; import io.netty.channel.simplechannelinboundhandler; import io.netty.channel.nio.nioeventloopgroup; import io.netty.channel.socket.datagramchannel; import io.netty.channel.socket.datagrampacket; import io.netty.channel.socket.internetprotocolfamily; import io.netty.channel.socket.nio.niodatagramchannel; import java.net.inetaddress; import java.net.inetsocketaddress; import java.net.networkinterface; public class mcast { private static final string local_addr = "192.168.0.18"; private static final string mcast_group = "239.254.42.96"; private static final int mcast_port = 9796; public static void main(string[] args) throws exception { thread sender = new thread(new sender()); thread receiver = new thread(new receiver()); receiver.start(); sender.start(); sender.join(); receiver.join(); } private static class mcastsupport { protected inetaddress localaddr; protected inetaddress remoteaddr; protected inetsocketaddress localsocketaddr; protected inetsocketaddress remotesocketaddr; protected datagramchannel chan; protected bootstrap bootstrap; public mcastsupport() { try { localaddr = inetaddress.getbyname(local_addr); remoteaddr = inetaddress.getbyname(mcast_group); localsocketaddr = new inetsocketaddress(localaddr, mcast_port); remotesocketaddr = new inetsocketaddress(remoteaddr, mcast_port); networkinterface nif = networkinterface.getbyinetaddress(localaddr); bootstrap = new bootstrap() .group(new nioeventloopgroup()) .handler(new simplechannelinboundhandler<datagrampacket>() { @override protected void messagereceived(channelhandlercontext ctx, datagrampacket msg) throws exception { system.out.println("received: " + msg.content().getint(0)); } }) .channelfactory(new channelfactory<niodatagramchannel>() { @override public niodatagramchannel newchannel() { return new niodatagramchannel(internetprotocolfamily.ipv4); } }) .handler(new simplechannelinboundhandler<datagrampacket>() { @override protected void messagereceived(channelhandlercontext ctx, datagrampacket msg) throws exception { system.out.println("received: " + msg.content().getint(0)); } }) .option(channeloption.so_broadcast, true) .option(channeloption.so_reuseaddr, true) .option(channeloption.ip_multicast_loop_disabled, false) .option(channeloption.so_rcvbuf, 2048) .option(channeloption.ip_multicast_ttl, 255) .option(channeloption.ip_multicast_if, nif); chan = (datagramchannel) bootstrap.bind(localsocketaddr).sync().channel(); chan.joingroup(remotesocketaddr, nif).sync(); } catch (throwable t) { system.err.println(t); t.printstacktrace(system.err); } } } private static class sender extends mcastsupport implements runnable { @override public void run() { try { (int seq = 1; seq <= 5; ++ seq) { bytebuf buf = unpooled.copyint(seq); datagrampacket dgram = new datagrampacket(buf, remotesocketaddr, localsocketaddr); chan.writeandflush(dgram); system.out.println("send: " + seq); thread.sleep(5000); } } catch (throwable t) { system.err.println(t); t.printstacktrace(system.err); } } } private static class receiver extends mcastsupport implements runnable { @override public void run() { try { thread.sleep(5 * 5000); } catch (throwable t) { system.err.println(t); t.printstacktrace(system.err); } } } }
the root of issue setting channeloption.ip_multicast_loop_disabled
false. leaving out line (i.e. allowing ip_multicast_loop_disabled
default true) allows multicast work expected. frankly, doesn't make lot of sense me, have no more time investigate.
Comments
Post a Comment