netty - 组播信道既可以接收也可以发送,但不能同时接收或发送

时间:2012-10-15 10:24:22

标签: java netty

我正在尝试使用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));

0 个答案:

没有答案