Netty 启动不了就vps

Netty not start on the vps

我无法使用 Netty 在我的 VPS 服务器上监听端口 1616 和所有其他端口。输入地址 xxx.xxx.xx.xxx:1616 时没有答案,如果我的 运行 服务器在我的计算机上,一切都可以完美收听, vps 服务器则不然。可悲的是,当我 运行 在 VPS 上使用 mvc 的项目时,它没有给出任何错误,似乎一切正常。我刚开始学netty。

ChatServer.java

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;


public class ChatServer {
    private final int port;
    ChatServer(int port){
        this.port=port;
    }
    public void run() throws InterruptedException {
        EventLoopGroup bossGroup=new NioEventLoopGroup();
        EventLoopGroup workerGroup=new NioEventLoopGroup();
        try{
            ServerBootstrap bootstrap=new ServerBootstrap().group(bossGroup,workerGroup).option(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.SO_KEEPALIVE, true).channel(NioServerSocketChannel.class).childHandler(new ChatServerIntializer());
            bootstrap.bind("localhost",port).sync().channel().closeFuture().sync();
        }
        finally {
            System.out.println("End.");
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
    public static void main(String args[]) throws InterruptedException {
        System.out.println("Starting..");
        System.out.println("1.3");
        new ChatServer(1616).run();
    }
}

ChatServerHandler.java


import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

class ChatServerHandler extends SimpleChannelInboundHandler<String> {
    private static final ChannelGroup channels=new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        Channel incoming=channelHandlerContext.channel();
        for(Channel channel : channels){
            channel.writeAndFlush("["+incoming.remoteAddress()+"]"+" write: "+s);

        }
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel incoming=ctx.channel();
        for(Channel channel : channels){
            if(incoming==channel){
                continue;
            }
            channel.writeAndFlush("["+incoming.remoteAddress()+"]"+" join in the chat");
        }
        channels.add(ctx.channel());
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel incoming=ctx.channel();
        for(Channel channel : channels){
            channel.writeAndFlush("["+incoming.remoteAddress()+"]"+" exit from the chat");
        }
        channels.remove(ctx.channel());
        super.handlerRemoved(ctx);
    }




}

ChatServerIntializer.java


package com.app;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

class ChatServerIntializer extends ChannelInitializer<SocketChannel> {
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline=socketChannel.pipeline();
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder",new StringEncoder());
        pipeline.addLast("handler",new ChatServerHandler());
    }
}

这就是我用命令mvn exec:java -Dexec.mainClass="com.app.ChatServer"

开始的全部

https://i.imgur.com/nB4cq4i.png

我想寻求解决方案或可能的原因

我知道了!

关闭防火墙对我有帮助

$ sudo iptables -t nat -F
$ sudo iptables -t nat -X
$ sudo iptables -t mangle -F
$ sudo iptables -t mangle -X
$ sudo iptables -P INPUT ACCEPT
$ sudo iptables -P FORWARD ACCEPT
$ sudo iptables -P OUTPUT ACCEPT```