Нетты не запускаются на vps - PullRequest
0 голосов
/ 13 июня 2019

Я не могу прослушивать порт 1616 и все остальные на моем VPS-сервере через Netty. При наборе адреса xxx.xxx.xx.xxx:1616 нет ответа, если я запускаю сервер на своем компьютере, все отлично слушается, а сервер vps - нет. Печально то, что когда я запускаю проект на VPS с использованием mvc, он не выдает никаких ошибок, кажется, что все работает хорошо. Я только начинаю изучать нетти.

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

Я хотел бы найти решение или возможные причины

1 Ответ

0 голосов
/ 15 июня 2019

Я понял!

Мне помогло отключение брандмауэра

$ 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```
...