Netty 关闭/停止 UDP 服务器

我正在尝试创建 Netty UDP 侦听器。我面临的问题是我无法像 tcp 一样停止 udp 服务器,udp 始终在运行,即使正常调用了关机,停止它的唯一方法是抛出异常。


private int port;

private UDPViewModel viewModel;

private DefaultEventLoopGroup defaultEventLoopGroup;


public UdpServer(UDPViewModel viewModel, int port) {

   this.port = port;

   this.viewModel = viewModel;

}


@Override

public void run() {

    defaultEventLoopGroup = new DefaultEventLoopGroup();

    try {


        ServerBootstrap bootstrap = new ServerBootstrap()

                .channel(UdpServerChannel.class)

                .group(defaultEventLoopGroup)

                .childHandler(new ChannelInitializer<Channel>() {

                    @Override

                    protected void initChannel(Channel channel) {

                        channel.pipeline()

                                .addLast(new ReadTimeoutHandler(5))

                                .addLast(new UdpServerHandler(viewModel));

                    }

                });

        bootstrap.bind(port).sync().channel().closeFuture().syncUninterruptibly().await();


        System.out.println("UDP Server : [successfully started]");

    } catch (InterruptedException e) {

        e.printStackTrace();

    } finally {

        defaultEventLoopGroup.shutdownGracefully();

    }

}

有人对如何正确关闭 netty udp 服务器有任何想法吗?


白板的微信
浏览 760回答 2
2回答

米琪卡哇伊

首先,我假设你使用这个git repo,顺便说一下,它写得很差。此外,我建议不要使用它,因为 UDP 不打算在服务器/客户端模型中使用,并且 repo 所做的只是管理您的 UDP 通道,这些通道不存在,因为 UDP 是无连接的。它真正做的就是存储一个假的通道实例,它的核心只是一个InetAddress.&nbsp;你可以做的是使用普通线程安全List或某种存储来InetAddress缓存不同的东西,然后使用它。但是如果你真的需要使用这个 repo,你需要停止ServerChannel实例,因为它UdpServerChannel启动了一个新的事件循环,它不会暴露在外面,只能在关闭通道时停止。(这是你不应该使用它的另一个原因,EventLoopGroups为同一件事打开多个是浪费的)

森林海

我想我找到了另一种方法来解决这个问题。我的问题的解决方案,它不是完美的,但它做了我想要的。public class UdpServer {private int port;private UDPViewModel viewModel;private final EventLoopGroup nioEventLoopGroup;private ChannelFuture channelFuture;public UdpServer(UDPViewModel viewModel, int port) {&nbsp; &nbsp;this.port = port;&nbsp; &nbsp;this.viewModel = viewModel;&nbsp; &nbsp; nioEventLoopGroup = new NioEventLoopGroup();}public void run() {&nbsp; &nbsp; System.out.println("UDP Server is starting.");&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; Bootstrap bootstrap = new Bootstrap();&nbsp; &nbsp; &nbsp; &nbsp; bootstrap.group(nioEventLoopGroup)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .channel(NioDatagramChannel.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .handler(new ChannelInitializer<Channel>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void initChannel(Channel channel) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.pipeline().addLast(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new LoggingHandler(LogLevel.INFO),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new StringEncoder(), new StringDecoder());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.pipeline().addLast(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new UdpServerHandler(viewModel));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; channelFuture = bootstrap.bind(port).sync();&nbsp; &nbsp; }&nbsp; &nbsp; catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println("UDP listener was interrupted and shutted down");&nbsp; &nbsp; &nbsp; &nbsp; e.getCause();&nbsp; &nbsp; }}public void StopServer(){&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; nioEventLoopGroup.shutdownGracefully().sync();&nbsp; &nbsp; &nbsp; &nbsp; channelFuture.channel().closeFuture().sync();&nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java