public class EchoServer {
private final int port;
public EchoServer(int port) {
this.port = port;
}
public static void main(String[] args) throws InterruptedException {
new EchoServer(80).start();
}
public void start() throws InterruptedException {
EchoServerHandler handler = new EchoServerHandler();
MyHandler myHandler = new MyHandler();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(handler).addLast(myHandler);
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully().sync();
}
}
}
@ChannelHandler.Sharable
public class MyHandler扩展了SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg)
throws Exception {
System.out.println(msg.refCnt());
}
}
这是我的代码,当服务器启动时,cmd-&gt; telnet localhost 80 程序异常 服务器收到:h 1 三月08,20188 10:34:04下午io.netty.util.ReferenceCountUtil safeRelease 警告:无法发布消息:PooledUnsafeDirectByteBuf(已释放) io.netty.util.IllegalReferenceCountException:refCnt:0,increment:1 at io.netty.buffer.AbstractReferenceCountedByteBuf.release0(AbstractReferenceCountedByteBuf.java:100) 在io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:84)
我不知道为什么