I'm working on Netty tutorials in youtube.
I've below server code -
package yt.ingrim.p01;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class Server {
public static class MySimpleChannelInboundHandler extends SimpleChannelInboundHandler<Object> {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
System.out.println("channel Connected --> " + ctx.channel());
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
}
}
public static final boolean EPOLL = Epoll.isAvailable();
Class channelClazz = EPOLL? EpollServerSocketChannel.class : NioServerSocketChannel.class;
public Server() throws Exception {
EventLoopGroup eventLoopGroup = EPOLL? new EpollEventLoopGroup(): new NioEventLoopGroup();
try {
new ServerBootstrap()
.group(eventLoopGroup)
.channel(channelClazz)
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline cpl = ch.pipeline();
cpl.addLast("default_channel_handler", new MySimpleChannelInboundHandler());
}
}).bind(8000).sync().channel().closeFuture().syncUninterruptibly();
} finally {
eventLoopGroup.shutdownGracefully().await();
}
}
public static void main(String[] args) throws Exception {
new Server();
}
}
The Client Code is -
package yt.ingrim.p01;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
public class Client {
public static final boolean EPOLL = Epoll.isAvailable();
Class channelClazz = EPOLL? EpollServerSocketChannel.class : NioServerSocketChannel.class;
public Client() throws Exception {
EventLoopGroup eventLoopGroup = EPOLL? new EpollEventLoopGroup(): new NioEventLoopGroup();
try {
new Bootstrap()
.group(eventLoopGroup)
.channel(channelClazz)
.remoteAddress(new InetSocketAddress("127.0.0.1", 8000))
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
}
}).connect().sync().channel().closeFuture().syncUninterruptibly();
} finally {
eventLoopGroup.shutdownGracefully().sync();
}
}
public static void main(String []args) throws Exception {
System.out.println(EPOLL);
new Client();
}
}
In my system EPOLL is false. When I run the server it is working fine.
When I run the client, I get the below error -
Exception in thread "main" java.lang.UnsupportedOperationException at io.netty.channel.socket.nio.NioServerSocketChannel.doConnect(NioServerSocketChannel.java:178) at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.connect(AbstractNioChannel.java:248) at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1342) at io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:548) at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:533) at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:517) at io.netty.channel.DefaultChannelPipeline.connect(DefaultChannelPipeline.java:978) at io.netty.channel.AbstractChannel.connect(AbstractChannel.java:265) at io.netty.bootstrap.Bootstrap$3.run(Bootstrap.java:250) at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:829)
My code is exactly same in the tutorial. What is the issue in my code. How can I fix this issue?
Netty version is - 4.1.82.Final Java Version is - 17
java --version
openjdk 17.0.3-oracle
My code is on github here