Netty 笔记 -- 记一次消息无法发送

客户端代码:



import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;


public class NettyClient {
    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup group = new NioEventLoopGroup();
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .remoteAddress("localhost", 7113)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInboundHandlerAdapter(){
                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println(">>>>> 消息发送..");
                        ctx.channel().writeAndFlush("sasaas");
                    }
                });

        try {
            Channel channel = bootstrap.connect().sync().channel();
            ChannelFuture future = channel.closeFuture().sync();
            future.addListener(ChannelFutureListener.CLOSE);
        } catch (InterruptedException e) {

        } finally {
            group.shutdownGracefully();
        }
    }
}

 

服务端代码:


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;



public class NettyServer {
    public static void main(String[] args) {
        ServerBootstrap bootstrap = new ServerBootstrap();
        EventLoopGroup group = new NioEventLoopGroup();
        bootstrap.group(group)
                .channel(NioServerSocketChannel.class)
                .childHandler(new LoggingHandler(LogLevel.DEBUG));
        try {
            ChannelFuture future = bootstrap.bind(7113).sync();
            future = future.channel().closeFuture().sync();
        } catch (InterruptedException e) {

        } finally {
            group.shutdownGracefully();
        }
    }
}

 

日志:

client 日志>>>>> 消息发送..

server 日志>>>>

原因:

最后传输的消息是bytebuf 或 fileRegion,其他消息不进行发送

Netty 笔记 -- 记一次消息无法发送_第1张图片

Netty 笔记 -- 记一次消息无法发送_第2张图片

你可能感兴趣的:(Netty,笔记,java,后端,netty)