简体   繁体   中英

Servers stops receiving UDP Multicast packets when I create multiple servers on same node

I'm using Netty for sending and receiving UDP Multicast messages and when I create multiple servers on same node it sometimes stops stop receiving packets from other servers. However even though they don't receive any packet from other servers, they still keep receiving packets that are sent using same multicast server.

Here is the server code:

multicastAddress = new InetSocketAddress("239.255.27.1", 14878);

Bootstrap a = new Bootstrap()
                .group(group)
                .channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4))
                .localAddress(multicastAddress)
                .option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF)
                .option(ChannelOption.SO_REUSEADDR, true)
                .handler(new ChannelInitializer<NioDatagramChannel>() {
                    @Override
                    public void initChannel(NioDatagramChannel ch) throws Exception {
                        h.pipeline().addLast(new ChannelInboundHandler() {
                              @Override
                              public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    System.out.println(msg);
                              }
                        });
                    }
                });

multicastServer = (NioDatagramChannel) a.bind().sync().channel();
multicastServer.joinGroup(multicastAddress, NetUtil.LOOPBACK_IF).sync();

The client periodically sends same ByteBuf:

multicastServer.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), multicastAddress));

When I start a server on a node, it starts receiving packets that are sent from that server. Then, if create the second server on the same node, it usually works and the server receives packets sent from both first and second server. However when I create a few more servers that are bound to same port (I use ChannelOption.SO_REUSEADDR so it should be a problem) sometimes all of the servers stop receiving packets from other servers instead only receive packets that are sent from the same server.

The problem is here:

.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF)

This tells UDP to only send membership reports to the localhost, so the other hosts don't know that this host is a member. Just remove it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM