客户端和服务端代码
package com.lyzx.netty.netty02;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.junit.Test;
/**
* @author hero.li
* 测试netty的拆包粘包问题
* 拆包粘包的出现的底层原因是
* TCP协议只是一个传输控制协议,TCP协议只需要把数据在客户端和服务端之间传输
* 而并不知道这些数据的具体含义,就像水流一样,只要把数据传输成功就完成任务
* 对于数据可能按照自己的一些规则组合更高效的传输,在组合的时候就无意中改变了
* 上层业务数据的"规则" 这就是拆包粘包问题
*
* 解决方案有3 种
* 1、分隔符
* 只需要在客户端和服务端的消息过滤器上加上
DelimiterBasedFrameDecoder的实现即可
* 注意:在发送消息时就需要加上分隔符(客户端和服务端都要加)
* 2、消息定长
* 3、自定义协议,报文头和报文体,其中报文头记录报文体的长度
*/
public class Netty02 {
@Test
public void nettyServer() throws InterruptedException{
//开启两个线程组,一个用于接受客户端的请求 另一个用于异步的网络IO的读写
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
//Netty启动的辅助类 里面封装了客户端和服务端的链接以及如何处理选择器 selector等逻辑
ServerBootstrap b = new ServerBootstrap();
//传入两个线程组,设置传输块大小为1k,
//添加ServerHandler类型的过滤器(表示如何处理这些消息,过滤器当然要集成netty的一个接口)
b.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,1024)
.childOption(ChannelOption.SO_KEEPALIVE,Boolean.FALSE)
.childHandler(new ChannelInitializer<SocketChannel>(){
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
//以$$_$$为分隔符,
ByteBuf delimiter = Unpooled.copiedBuffer("$$_$$".getBytes("UTF-8"));
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
socketChannel.pipeline().addLast(new StringEncoder());
socketChannel.pipeline().addLast(new ServerHandler());
}
});
//同步等待绑定端口结束
ChannelFuture f = b.bind(9988).sync();
//等待服务端监听端口关闭
f.channel().closeFuture().sync();
//优雅的关闭线程组
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
@Test
public void nettyClient() throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY,true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception{
ByteBuf delimiter = Unpooled.copiedBuffer("$$_$$".getBytes("UTF-8"));
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture f = b.connect("127.0.0.1", 9988).sync();
f.channel().closeFuture().sync();
group.shutdownGracefully();
}
}
handler代码如下
package com.lyzx.netty.netty02;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* 对于网事件做读写,通常只要关注channelRead()和exceptionCaught()即可
*/
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("server:channelRead 通道可读开始");
//从msg对象中获取消息并打印
ByteBuf bytebuf = (ByteBuf)msg;
byte[] bytes = new byte[bytebuf.readableBytes()];
bytebuf.readBytes(bytes);
System.out.println("收到的消息:"+new String(bytes,"UTF-8"));
//获取当前的时间并通过ctx的write方法异步写数据到SocketChannel
//ctx.write并不会直接把数据写入到缓冲区等到调用channelReadComplete里面的ctx.flush()
//的时候再把数写入到SocketChannel
String datetime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
ByteBuf byteBuf = Unpooled.copiedBuffer((datetime+"$$_$$").getBytes("UTF-8"));
ctx.writeAndFlush(byteBuf);
System.out.println("server:channelRead 通道可读结束");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("server:channelReadComplete 通道可读完成 ");
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("server:exceptionCaught 发生异常");
ctx.close();
}
}
客户端Handler
package com.lyzx.netty.netty02;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("client:channelActive 通道激活开始");
for(int i=0;i<10;i++){
//以$$_$$为分隔符,所以在每次发送的时候都需要以消息末尾加上$$_$$
ByteBuf buff = Unpooled.copiedBuffer(("req_"+i+"$$_$$").getBytes("UTF-8"));
ctx.writeAndFlush(buff);
}
System.out.println("client:channelActive 通道激活结束");
}
@Override
public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception {
System.out.println("client:通道可读开始");
ByteBuf buf = (ByteBuf)msg;
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
System.out.println("response time :"+new String(bytes));
System.out.println("client:通道可读结束");
}
/**
* 类似于AOP的后置通知 在这里当通道读取完毕后关闭通道
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("client:通道可读完成");
// ctx.channel().close();
// ctx.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("client:发生异常");
super.exceptionCaught(ctx, cause);
}
}
完整代码欢迎访问我的github https://github.com/lyzxhero/Netty