我无法使用 Netty 监听 VPS 服务器上的端口 1616 和所有其他端口。当输入地址 xxx.xxx.xx.xxx:1616 时没有答案,如果我在计算机上运行服务器,一切都会被完美监听,而 vps 服务器则不然。可悲的是,当我使用 mvc 在 VPS 上运行项目时,它没有给出任何错误,似乎一切正常。我刚刚开始学习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"
我想找到解决方案或可能的原因
最佳答案
我明白了!
关闭防火墙对我有帮助
$ 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```
关于java - Netty无法在vps上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572066/