Reactor-Netty:是否同时监听IPv4和IPv6?

问题描述

是否有可能让反应堆网络监听IPv4和IPv6 UDP流量?

我们有一个旧版应用程序,我想使用反应器网际网络协议TCP / UDP进行连接处理,而不是使用原始Java套接字等进行重写。我可以直接使用Netty,但是就像Reactor-给我的响应流一样净额。

感谢@Violeta帮助我解决了有关如何使用多播UDP的最初问题。

现在,我有以下代码将所有通过IPv4的网络接口绑定到多播组224.0.0.224,并且我可以很好地接收消息。

我现在想建立另一个连接,该连接执行相同的处理,但使用IPv6和多播组FF02::1

我尝试使用另一个调用.runOn(resources,InternetProtocolFamily.IPv6)的方法,但这引发了异常,因为我已经事先调用了该方法。

因此,我刚刚创建了我的代码的另一个实例,并将其更改为可与IPv6一起使用。

代码似乎可以正常工作,我在IPv4和IPv6上都获得了流量,并且只是想知道这是否是设置这种情况的正确方法。

我们需要侦听计算机中的所有接口,并侦听端口35057并加入多播组。

在Vert.x中,我通过定义单独的DatagramChannels来实现相同的目的,每个DatagramChannels都有自己的通道选项集,并且由于Vert.x在幕后使用Netty,所以我假设我必须创建两个Connection实例已经有。

谢谢..

这是当前正在工作的代码。(减去一些util方法)

public class Application {

    private final static Logger LOG = LoggerFactory.getLogger(Application.class);
    protected final static String IP4_MULTICAST_GROUP = "224.0.0.224";
    protected final static String IP6_MULTICAST_GROUP = "FF02::1";

    public static void main(String[] args) throws SocketException,UnknownHostException {

        Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
        final InetAddress multicastGroup = InetAddress.getByName(IP4_MULTICAST_GROUP);
        final InetAddress multicastGroup6 = InetAddress.getByName(IP6_MULTICAST_GROUP);
        final NetworkInterface multicastInterface = NetUtils.mainInterface();
        LOG.info("Using network interface '{}' for multicast",multicastInterface);

        LoopResources resources = LoopResources.create("test");
        Connection ipv4 =
                UdpServer.create()
                        .bindAddress(() -> new InetSocketAddress(35057))
                        .option(ChannelOption.SO_REUSEADDR,true)
                        .doOnBound(conn -> conn.addHandler(new UdpDecoderHandler()))
                        .runOn(resources,InternetProtocolFamily.IPv4)
                        .handle((in,out) -> Flux.<NetworkInterface>generate(s -> {
                            if (ifaces.hasMoreElements()) {
                                s.next(ifaces.nextElement());
                            } else {
                                s.complete();
                            }
                        }).flatMap(iface -> {
                            if (NetUtils.isMulticastEnabledIPv4Interface(iface)) {
                                LOG.info("Joining iFace {} to Multicast Group {} ",iface,IP4_MULTICAST_GROUP);
                                return in.join(multicastGroup,iface);
                            }
                            return Flux.empty();
                        }).thenMany(
                                out.sendObject(
                                        in.receiveObject().flatMap(o -> {
                                            if (o instanceof SupDataInfo) {
                                                return parseAndBuildResponse(o);
                                            } else {
                                                return Mono.error(new Exception("Invalid Payload: " + o));
                                            }
                                        }))
                        ).onErrorContinue((error,obj) -> LOG.error("Opps: {}",obj,error)))
                        .bindNow(Duration.ofSeconds(30));

        ipv4.onDispose();


        Connection ipv6 =
                UdpServer.create()
                        .bindAddress(() -> new InetSocketAddress(35057))
                        .option(ChannelOption.SO_REUSEADDR,true)
                        .option(ChannelOption.IP_MULTICAST_IF,multicastInterface)
                        .doOnBound(conn -> conn.addHandler(new UdpDecoderHandler()))
                        .runOn(resources,InternetProtocolFamily.IPv6)
                        .handle((in,out) -> Flux.<NetworkInterface>generate(s -> {
                            if (ifaces.hasMoreElements()) {
                                s.next(ifaces.nextElement());
                            } else {
                                s.complete();
                            }
                        }).flatMap(iface -> {
                            if (NetUtils.isMulticastEnabledIPv6Interface(iface)) {
                                LOG.info("Joining iFace {} to Multicast Group {} ",IP6_MULTICAST_GROUP);
                                return in.join(multicastGroup6,error)))
                        .bindNow(Duration.ofSeconds(30));

        ipv6.onDispose()
                .block();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...