我正在尝试使用 netty 创建一个多播通道,我可以使用它发送和接收多播组的消息。
我像这样创建了引导实例
channelFactory = new OioDatagramChannelFactory( workerExecutor );
bootstrap = new ConnectionlessBootstrap( channelFactory );
bootstrap.setOption( "broadcast", true );
bootstrap.setOption( "reuseAddress", true );
稍后频道是这样创建的
DatagramChannel channel;
channel = (DatagramChannel) bootstrap.bind( new InetSocketAddress( mcastPort) );
channel.joinGroup( multicastAddress );
使用此代码,我可以接收多播组的消息,但一个简单channel.write();
的 NPE 失败。
java.lang.NullPointerException: null address || null buffer
at java.net.TwoStacksPlainDatagramSocketImpl.send(Native Method)
at java.net.DatagramSocket.send(DatagramSocket.java:675)
at org.jboss.netty.channel.socket.oio.OioDatagramWorker.write(OioDatagramWorker.java:121)
at org.jboss.netty.channel.socket.oio.OioDatagramPipelineSink.eventSunk(OioDatagramPipelineSink.java:84)
at *.*.*PacketHandler.writeRequested(*PacketHandler.java:72)
at org.jboss.netty.channel.Channels.write(Channels.java:611)
at org.jboss.netty.channel.Channels.write(Channels.java:578)
at org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:251)
at org.jboss.netty.channel.group.DefaultChannelGroup.write(DefaultChannelGroup.java:261)
由于异常表明事件没有指定远程地址。
因为我不想在我调用的每个写入请求上传递多播地址Channel#connect
。
DatagramChannel channel;
channel = (DatagramChannel) bootstrap.bind( new InetSocketAddress( mcastPort) );
channel.joinGroup( multicastAddress );
channel.connect( new InetSocketAddress(multicastAddress, mcastPort) );
现在发送消息完美无缺,但我不再收到消息。
我究竟做错了什么?我正在使用 netty 3.2.7.Final
编辑
以下是我对所描述的 netty 行为的期望。
它们是有效的还是我误解了什么?
// DatagramSocket bound to network interface "192.168.0.15" and port 12345
channel = (DatagramChannel) bootstrap.bind( new InetSocketAddress( "192.168.0.15", 12345) );
// Send IGMP Join to "239.195.255.255" in order to receive multicast packets
channel.joinGroup("239.195.255.255");
// Possibility 1
// Channels remote address gets set
channel.connect(new InetSocketAddress("239.195.255.255", 12345));
// packets gets send over interface "192.168.0.15" port 12345 to "239.195.255.255:12345"
channel.write(something);
// Possibility 2
// Send packet to explicit address
channel.write(something, new InetSocketAddress("239.195.255.255", 12345));