当我使用 Netty 在此端口上创建 UDP 监视器时,为什么端口状态不是监听?

问题描述

这是创建频道的代码

 public class StunServer {

    private StunServer(){
        boolean win = GeneralUtil.windows();
        if(win){
            System.out.println("windows");
            this.group = new NioEventLoopGroup();
        }else{
            System.out.println("linux");
            this.group = new EpollEventLoopGroup();
        }
        this.bootstrap = new Bootstrap();
        bootstrap.group(group).channel(win?NioDatagramChannel.class:EpollDatagramChannel.class)
//                .option(ChannelOption.so_broADCAST,true)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        channel.pipeline()
                                .addLast(new StunUdpMsgDecoder())
                                .addLast(new StunUdpMsgEncoder())
                                .addLast(new StunUdpMsgHandler());
                    }
                });
    }

    private static StunServer instance = new StunServer();

    public static StunServer getInstance(){
        return instance;
    }

    private final Bootstrap bootstrap;
    private final EventLoopGroup group;


    public Channel getChannel(int port){
        System.out.println("bind port:"+port);
        ChannelFuture channelFuture = bootstrap.bind(port).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if(!channelFuture.isSuccess()){
                    channelFuture.cause().printstacktrace();
                    channelFuture.channel().close();
                }
            }
        }).syncUninterruptibly();
        return channelFuture.channel();
    }

    public void stop(){
        group.shutdownGracefully().syncUninterruptibly();
    }
}

这是主要方法

public static void main(String[] args) throws Exception{
        StunServer server = StunServer.getInstance();
        try {
            Channel channel = server.getChannel(VarEnums.server1.getPort());
            channel.closeFuture().sync();
        }catch(Exception  e){
            e.printstacktrace();
        }finally {
            server.stop();
        }
    }

很简单,我很确定消息发送成功。但是解码器没有被执行。 发现端口没有监听。

netstat -nap | grep java

如你所见。它应该是这样的?我猜。

img

无论如何,伙计们帮助我,谢谢。

解决方法

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

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

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