linux安装netty服务器,Netty 4在Linux上接收多播数据包

我写了一个应用程序接收发件人发送的组播数据包(包含audio)。 我已经使用Netty 4,并已经在Windows上运行该应用程序,但是在Linux(Debian Wheezy(raspi)和Ubuntu 12)上运行时,它不会收到多播数据包。

我创build了一些可以发送和接收多播数据包的testing代码,结果是:

发送Windows到Windows的作品。

发送Linux到Windows的作品。

发送Windows到Linux,数据包发送但没有收到。

我以root身份运行该应用程序,并将SO_BROADCAST设置为true。

我错过了什么?

如果我使用标准的Java MulticastSocket而不是Netty,那么这个应用程序就可以工作,但是我更喜欢使用Netty,因为它很容易使用并且大大简化了代码。

testing代码是:

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() { @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); } }

}

编辑:

如果发现我的问题,我应该学会阅读文档!

对于Mutlicast,您应该绑定到WILDCARD地址。

所以改变代码

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()); }

我已经修改了上面的完整代码和新的变化..

皮特。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值