| 1) TCP是面向连接的,面向流的,提供高可靠性服务。收发两端(客户端和服务器端)都要有一一成对的socket,因此,发送端为了将多个发给接收端的包,更有效的发给对方,使用了优化方法(Nagle算法),将多次间隔较小且数据量小的数据, |
| 合并成一个大的数据块,然后进行封包。这样做虽然提高了效率,但是接收端就难于分辨出完整的数据包了,因为面向流的通信是无消息保护边界的 |
| 2) 由于TCP无消息保护边界, 需要在接收端处理消息边界问题,也就是我们所说的粘包、拆包问题 |

| 假设客户端分别发送了两个数据包D1和D2给服务端,由于服务端一次读取到字节数是不确定的,故可能存在以下四种情况: |
| 1) 服务端分两次读取到了两个独立的数据包,分别是D1和D2,没有粘包和拆包 |
| 2) 服务端一次接受到了两个数据包,D1和D2粘合在一起,称之为TCP粘包 |
| 3) 服务端分两次读取到了数据包,第一次读取到了完整的D1包和D2包的部分内容,第二次读取到了D2包的剩余内容,这称之为TCP拆包 |
| 4) 服务端分两次读取到了数据包,第一次读取到了D1包的部分内容D1_1,第二次读取到了D1包的剩余部分内容D1_2和完整的D2包 |
| public class MyServer { |
| public static void main(String[] args) throws Exception{ |
| |
| EventLoopGroup bossGroup = new NioEventLoopGroup(1); |
| EventLoopGroup workerGroup = new NioEventLoopGroup(); |
| |
| try { |
| |
| ServerBootstrap serverBootstrap = new ServerBootstrap(); |
| serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler( |
| new com.ychen.netty.netty.tcp.MyServerInitializer()); |
| |
| ChannelFuture channelFuture = serverBootstrap.bind(7000).sync(); |
| channelFuture.channel().closeFuture().sync(); |
| |
| }finally { |
| bossGroup.shutdownGracefully(); |
| workerGroup.shutdownGracefully(); |
| } |
| |
| } |
| } |
| |
| public class MyServerInitializer extends ChannelInitializer<SocketChannel> { |
| |
| @Override |
| protected void initChannel(SocketChannel ch) throws Exception { |
| ChannelPipeline pipeline = ch.pipeline(); |
| pipeline.addLast(new MyServerHandler()); |
| } |
| } |
| |
| public class MyServerHandler extends SimpleChannelInboundHandler<ByteBuf>{ |
| |
| private int count; |
| |
| @Override |
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| cause.printStackTrace(); |
| ctx.close(); |
| } |
| |
| @Override |
| protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { |
| |
| byte[] buffer = new byte[msg.readableBytes()]; |
| msg.readBytes(buffer); |
| |
| |
| String message = new String(buffer, Charset.forName("utf-8")); |
| |
| System.out.println("服务器接收到数据 " + message); |
| System.out.println("服务器接收到消息量=" + (++this.count)); |
| |
| |
| ByteBuf responseByteBuf = Unpooled.copiedBuffer(UUID.randomUUID().toString() + " ", Charset.forName("utf-8")); |
| ctx.writeAndFlush(responseByteBuf); |
| |
| } |
| } |
| public class MyClient { |
| public static void main(String[] args) throws Exception{ |
| |
| EventLoopGroup group = new NioEventLoopGroup(); |
| |
| try { |
| |
| Bootstrap bootstrap = new Bootstrap(); |
| bootstrap.group(group).channel(NioSocketChannel.class) |
| .handler(new com.ychen.netty.netty.tcp.MyClientInitializer()); |
| |
| ChannelFuture channelFuture = bootstrap.connect("localhost", 7000).sync(); |
| channelFuture.channel().closeFuture().sync(); |
| |
| }finally { |
| group.shutdownGracefully(); |
| } |
| } |
| } |
| |
| public class MyClientInitializer extends ChannelInitializer<SocketChannel> { |
| @Override |
| protected void initChannel(SocketChannel ch) throws Exception { |
| |
| ChannelPipeline pipeline = ch.pipeline(); |
| pipeline.addLast(new MyClientHandler()); |
| } |
| } |
| |
| public class MyClientHandler extends SimpleChannelInboundHandler<ByteBuf> { |
| |
| private int count; |
| |
| @Override |
| public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| |
| for(int i= 0; i< 10; ++i) { |
| ByteBuf buffer = Unpooled.copiedBuffer("hello,server " + i, Charset.forName("utf-8")); |
| ctx.writeAndFlush(buffer); |
| } |
| } |
| |
| @Override |
| protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { |
| |
| byte[] buffer = new byte[msg.readableBytes()]; |
| msg.readBytes(buffer); |
| |
| String message = new String(buffer, Charset.forName("utf-8")); |
| System.out.println("客户端接收到消息=" + message); |
| System.out.println("客户端接收消息数量=" + (++this.count)); |
| } |
| |
| @Override |
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| cause.printStackTrace(); |
| ctx.close(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-08-11 SpringSecurity入门
2021-08-11 gradle入门