Java Netty 4在Linux上接收多播数据包
我写了一个应用程序,它接收发送者发送的多播数据包(包含音频)。 我已经使用了Netty 4,并让该应用程序在Windows上运行,但在Linux(Debian Wheezy(raspi)和Ubuntu 12)上运行时,它不会接收多播数据包 我创建了一些可以发送和接收多播数据包的测试代码,结果如下: 将Windows发送到Windows works 将Linux发送到Windows works 将Windows发送到Linux,数据包已发送但未接收 我以root用户身份运行应用程序,并将SO_BROADCAST设置为true 我错过了什么 如果我使用标准的Java MulticastSocket而不是Netty,那么应用程序就可以运行了,但我更喜欢使用Netty,因为它易于使用,并且大大简化了代码 测试代码为:Java Netty 4在Linux上接收多播数据包,java,linux,raspberry-pi,netty,raspbian,Java,Linux,Raspberry Pi,Netty,Raspbian,我写了一个应用程序,它接收发送者发送的多播数据包(包含音频)。 我已经使用了Netty 4,并让该应用程序在Windows上运行,但在Linux(Debian Wheezy(raspi)和Ubuntu 12)上运行时,它不会接收多播数据包 我创建了一些可以发送和接收多播数据包的测试代码,结果如下: 将Windows发送到Windows works 将Linux发送到Windows works 将Windows发送到Linux,数据包已发送但未接收 我以root用户身份运行应用程序,并将SO_BR
public class TestMulticast {
private int port = 51972;
private Logger log = Logger.getLogger(this.getClass());
private InetAddress remoteInetAddr = null;
private InetSocketAddress remoteInetSocket = null;
private InetAddress localInetAddr = null;
private InetSocketAddress localInetSocket = null;
private DatagramChannel ch = null;
private EventLoopGroup group = new NioEventLoopGroup();
private boolean bSend = false;
public TestMulticast(String localAddress, String remoteAddress, String sPort, boolean bSend) {
this.bSend = bSend;
try {
localInetAddr = InetAddress.getByName(localAddress.trim());
remoteInetAddr = InetAddress.getByName(remoteAddress.trim());
} catch (Exception e) {
log.error("Error creating InetAddresses. Local: " + localAddress + " Remote: " + remoteAddress, e);
}
try {
port = Integer.parseInt(sPort);
} catch (Exception e) {
log.error("Error Parsing Port: " + sPort, e);
}
}
public void run() throws Exception {
log.debug("Run TestMulticast, Send Packet = " + bSend);
try {
localInetSocket = new InetSocketAddress(port);
remoteInetSocket = new InetSocketAddress(remoteInetAddr, port);
Bootstrap b = new Bootstrap();
b.group(group);
b.channelFactory(new ChannelFactory<Channel>() {
@Override
public Channel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
});
b.option(ChannelOption.SO_BROADCAST, true);
b.option(ChannelOption.SO_REUSEADDR, true);
b.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, false);
b.option(ChannelOption.SO_RCVBUF, 2048);
b.option(ChannelOption.IP_MULTICAST_TTL, 255);
b.handler(new LoggingHandler(LogLevel.DEBUG));
log.debug("Am I Logged on as ROOT: " + PlatformDependent.isRoot());
ch = (DatagramChannel) b.bind(localInetSocket).sync().channel();
log.debug("Result of BIND: " + ch.toString());
if (remoteInetAddr.isMulticastAddress()) {
NetworkInterface nic = NetworkInterface.getByInetAddress(localInetAddr);
ChannelFuture future = ch.joinGroup(remoteInetSocket, nic);
log.debug("Result of Join: " + future.toString());
} else {
log.debug("############NOT A MULTICAST ADDRESS: '" + remoteInetAddr.getHostAddress() + "'");
}
if (bSend) {
group.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
Date date = new Date();
byte[] bytes = date.toString().getBytes();
ByteBuf buffer = Unpooled.copiedBuffer(bytes);
DatagramPacket packet = new DatagramPacket(buffer, remoteInetSocket, localInetSocket);
ch.writeAndFlush(packet);
} catch (Exception e) {
log.error("Error Sending DatagramPacket", e);
}
}
}, 0, 10, TimeUnit.SECONDS);
}
} catch (Exception e) {
log.error(e);
}
}
public void stop() {
try {
if (ch != null) {
try {
ch.close();
} catch (Exception e) {
log.error("Error Closing Channel", e);
}
}
group.shutdownGracefully();
} catch (Exception e) {
log.error("Error ShuutingDown", e);
}
}
我已经用新的更改修改了上面的完整代码
皮特
localInetSocket = new InetSocketAddress(remotePort);
ch = (DatagramChannel) b.bind(localInetSocket).sync().channel();
if (remoteInetAddr.isMulticastAddress()) {
NetworkInterface nic = NetworkInterface.getByInetAddress(localInetAddr);
ChannelFuture future = ch.joinGroup(remoteInetSocket, nic);
log.debug("Result of Join: " + future.toString());
}