第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Netty 關(guān)閉/停止 UDP 服務器

Netty 關(guān)閉/停止 UDP 服務器

白板的微信 2021-12-01 16:09:43
我正在嘗試創(chuàng)建 Netty UDP 偵聽器。我面臨的問題是我無法像 tcp 一樣停止 udp 服務器,udp 始終在運行,即使正常調(diào)用了關(guān)機,停止它的唯一方法是拋出異常。private int port;private UDPViewModel viewModel;private DefaultEventLoopGroup defaultEventLoopGroup;public UdpServer(UDPViewModel viewModel, int port) {   this.port = port;   this.viewModel = viewModel;}@Overridepublic 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();    }}有人對如何正確關(guān)閉 netty udp 服務器有任何想法嗎?
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻1998條經(jīng)驗 獲得超6個贊

首先,我假設你使用這個git repo,順便說一下,它寫得很差。此外,我建議不要使用它,因為 UDP 不打算在服務器/客戶端模型中使用,并且 repo 所做的只是管理您的 UDP 通道,這些通道不存在,因為 UDP 是無連接的。它真正做的就是存儲一個假的通道實例,它的核心只是一個InetAddress. 你可以做的是使用普通線程安全List或某種存儲來InetAddress緩存不同的東西,然后使用它。

但是如果你真的需要使用這個 repo,你需要停止ServerChannel實例,因為它UdpServerChannel啟動了一個新的事件循環(huán),它不會暴露在外面,只能在關(guān)閉通道時停止。(這是你不應該使用它的另一個原因,EventLoopGroups為同一件事打開多個是浪費的)


查看完整回答
反對 回復 2021-12-01
?
森林海

TA貢獻2011條經(jīng)驗 獲得超2個贊

我想我找到了另一種方法來解決這個問題。我的問題的解決方案,它不是完美的,但它做了我想要的。


public class UdpServer {


private int port;

private UDPViewModel viewModel;

private final EventLoopGroup nioEventLoopGroup;

private ChannelFuture channelFuture;


public UdpServer(UDPViewModel viewModel, int port) {

   this.port = port;

   this.viewModel = viewModel;

    nioEventLoopGroup = new NioEventLoopGroup();

}


public void run() {

    System.out.println("UDP Server is starting.");

    try{

        Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(nioEventLoopGroup)

                .channel(NioDatagramChannel.class)

                .handler(new ChannelInitializer<Channel>() {

                    @Override

                    protected void initChannel(Channel channel) {

                        channel.pipeline().addLast(

                                new LoggingHandler(LogLevel.INFO),

                                new StringEncoder(), new StringDecoder());

                        channel.pipeline().addLast(

                                new UdpServerHandler(viewModel));

                    }

                });

        channelFuture = bootstrap.bind(port).sync();


    }

    catch (InterruptedException e) {

        System.err.println("UDP listener was interrupted and shutted down");

        e.getCause();

    }

}


public void StopServer()

{

    try {

        nioEventLoopGroup.shutdownGracefully().sync();

        channelFuture.channel().closeFuture().sync();

    } catch (InterruptedException e) {

        e.printStackTrace();

    }

}

}



查看完整回答
反對 回復 2021-12-01
  • 2 回答
  • 0 關(guān)注
  • 811 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號