I can’t listen to port 1616 and all others on my VPS server using Netty. When typing the address xxx.xxx.xx.xxx:1616 there is no answer, if I run server on my computer, everything is perfectly listened to, the vps server is not. The sad thing is that when I run a project on a VPS using mvc, it does not give any errors, it seems that everything works well. I'm just starting to learn 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());
}
}
This is all I start with the command mvn exec:java -Dexec.mainClass="com.app.ChatServer"
https://i.imgur.com/nB4cq4i.png
I would like to find a solution or possible reasons
localhost
, this means that you can only connect fromlocalhost
, I assume this is not intentional?