Netty-LengthFieldBasedFrameDecoder-解决拆包粘包问题的解码器

这篇具有很好参考价值的文章主要介绍了Netty-LengthFieldBasedFrameDecoder-解决拆包粘包问题的解码器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

构造器参数

  • maxFrameLength:指定解码器所能处理的数据包的最大长度,超过该长度则抛出 TooLongFrameException 异常;
  • lengthFieldOffset:指定长度字段的起始位置;
  • lengthFieldLength:指定长度字段的长度:目前支持1(byte)、2(short)、3(3个byte)、4(int)、8(Long)
  • lengthAdjustment:指定长度字段所表示的消息长度值与实际长度值之间的差值,可以用于调整解码器的计算和提高灵活性。
  • initialBytesToStrip:指定解码器在将数据包分离出来后,跳过的字节数,因为这些字节通常不属于消息体内容,而是协议头或其他控制信息。
  • lengthAdjustment(可正可负)需要满足以下的计算关系:
    Netty-LengthFieldBasedFrameDecoder-解决拆包粘包问题的解码器

单元测试Demo

有关使用方法和方式参考测试Demo即可。文章来源地址https://www.toymoban.com/news/detail-530581.html

package com.netty.framework.core.channel;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

/**
 * @author BugsHunter
 * 
 * @date 2023/7/7
 * @time 16:10
 */
public class LengthFieldBasedFrameDecoderTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(LengthFieldBasedFrameDecoderTest.class);

    // 用于输出内容
    private final ChannelInboundHandlerAdapter handlerAdapter = new ChannelInboundHandlerAdapter() {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            LOGGER.debug("handlerAdapter-接收到信息:{}", msg);
            super.channelRead(ctx, msg);
        }
    };

    /**
     * 二进制数据结构
     * lengthFieldOffset   = 0
     * lengthFieldLength   = 4
     * lengthAdjustment    = 0
     * initialBytesToStrip = 0 (不跳过长度字段长度信息读取)
     * 编码前 (16 bytes)                     编码后 (16 bytes)
     * +------------+----------------+      +------------+----------------+
     * |   Length   | Actual Content |----->|   Length   | Actual Content |
     * | 0x0000000C | "Hello, World" |      | 0x0000000C | "Hello, World" |
     * +------------+----------------+      +------------+----------------+
     */
    @Test
    public void test00() {
        class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
            public LengthFieldDecoder() {
                super(1024, 0, 4, 0, 0);
            }

            @Override
            protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                // 解码,得到解码后的字节数据(frame length=数据帧的长度)
                ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
                LOGGER.debug("长度读取前readerIndex={}", byteBuf.readerIndex());

                // readInt会更新readerIndex值
                int lengthField = byteBuf.readInt();
                LOGGER.debug("长度字段[LengthField]的值={},即(Hello, World)的二进制长度。", lengthField);
                LOGGER.debug("长度读取后readerIndex={}", byteBuf.readerIndex());

                // toString方法根据readerIndex最新值开始解码
                return byteBuf.toString(StandardCharsets.UTF_8);
            }
        }

        LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
        EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);

        byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);

        channel.writeInbound(byteBuf);
    }


    /**
     * 二进制数据结构
     * lengthFieldOffset   = 0
     * lengthFieldLength   = 4
     * lengthAdjustment    = 0
     * initialBytesToStrip = 4 (跳过长度字段长度信息读取)
     * 编码前 (16 bytes)                     编码后 (12 bytes)
     * +------------+----------------+      +--------+--------+
     * |   Length   | Actual Content |----->|  Actual Content |
     * | 0x0000000C | "Hello, World" |      |  "Hello, World" |
     * +------------+----------------+      +--------+--------+
     */
    @Test
    public void test01() {
        class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
            public LengthFieldDecoder() {
                super(1024, 0, 4, 0, 4);
            }

            @Override
            protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
                return byteBuf.toString(StandardCharsets.UTF_8);
            }
        }

        LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
        EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);

        byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);

        channel.writeInbound(byteBuf);
    }

    /**
     * 此处lengthField包含了消息头的长度,即:Length = lengthFieldLength + 消息字节长度(Hello, World的二进制长度)
     * lengthFieldOffset   =  0
     * lengthFieldLength   =  4
     * lengthAdjustment    = -4 (lengthField字段的长度)
     * initialBytesToStrip =  0
     * 编码前 (16 bytes)                     编码后 (16 bytes)
     * +------------+----------------+      +------------+----------------+
     * |   Length   | Actual Content |----->|   Length   | Actual Content |
     * | 0x0000000G | "Hello, World" |      | 0x0000000G | "Hello, World" |
     * +------------+----------------+      +------------+----------------+
     */
    @Test
    public void test02() {
        class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
            public LengthFieldDecoder() {
                super(1024, 0, 4, -4, 0);
            }

            @Override
            protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
                LOGGER.debug("长度读取前readerIndex={}", byteBuf.readerIndex());

                // readInt会更新readerIndex值
                int lengthField = byteBuf.readInt();
                LOGGER.debug("长度字段[LengthField]的值={},即(Hello, World)的二进制长度。", lengthField);
                LOGGER.debug("长度读取后readerIndex={}", byteBuf.readerIndex());

                // toString方法根据readerIndex最新值开始解码
                return byteBuf.toString(StandardCharsets.UTF_8);
            }
        }

        LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
        EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);

        byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = Unpooled.buffer();
        // 4=int的字节长度
        byteBuf.writeInt(bytes.length + 4);
        byteBuf.writeBytes(bytes);

        channel.writeInbound(byteBuf);
    }

    /**
     * lengthField在中间位置
     * lengthFieldOffset   = 2 (= the length of Header 1)
     * lengthFieldLength   = 4
     * lengthAdjustment    = 0
     * initialBytesToStrip = 0
     * <p>
     * BEFORE DECODE (18 bytes)                      AFTER DECODE (18 bytes)
     * +----------+----------+----------------+      +----------+----------+----------------+
     * | Header 1 |  Length  | Actual Content |----->| Header 1 |  Length  | Actual Content |
     * |  0xCAFE  | 0x00000C | "Hello, World" |      |  0xCAFE  | 0x00000C | "Hello, World" |
     * +----------+----------+----------------+      +----------+----------+----------------+
     */
    @Test
    public void test03() {
        class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
            public LengthFieldDecoder() {
                super(1024, 2, 4, 0, 0);
            }

            @Override
            protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
                LOGGER.debug("长度读取前readerIndex={}", byteBuf.readerIndex());

                // 读取header1
                short header1 = byteBuf.readShort();
                LOGGER.debug("header1={}", header1);

                // readInt会更新readerIndex值
                int lengthField = byteBuf.readInt();
                LOGGER.debug("长度字段[LengthField]的值={},即(Hello, World)的二进制长度。", lengthField);
                LOGGER.debug("长度读取后readerIndex={}", byteBuf.readerIndex());

                // toString方法根据readerIndex最新值开始解码
                return byteBuf.toString(StandardCharsets.UTF_8);
            }
        }

        LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
        EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);

        byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeShort(99);
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);

        channel.writeInbound(byteBuf);
    }

    /**
     * lengthField在开始位置
     * lengthFieldOffset   = 0 (= the length of Header 1)
     * lengthFieldLength   = 4
     * lengthAdjustment    = 2
     * initialBytesToStrip = 0
     * BEFORE DECODE (18 bytes)                      AFTER DECODE (18 bytes)
     * +----------+----------+----------------+      +----------+----------+----------------+
     * |  Length  | Header 1 | Actual Content |----->|  Length  | Header 1 | Actual Content |
     * | 0x00000C |  0xCAFE  | "Hello, World" |      | 0x00000C |  0xCAFE  | "Hello, World" |
     * +----------+----------+----------------+      +----------+----------+----------------+
     */
    @Test
    public void test04() {
        class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
            public LengthFieldDecoder() {
                super(1024, 0, 4, 2, 0);
            }

            @Override
            protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
                LOGGER.debug("长度读取前readerIndex={}", byteBuf.readerIndex());

                // readInt会更新readerIndex值
                int lengthField = byteBuf.readInt();
                LOGGER.debug("长度字段[LengthField]的值={},即(Hello, World)的二进制长度。", lengthField);
                LOGGER.debug("长度读取后readerIndex={}", byteBuf.readerIndex());

                // 读取header1
                short header1 = byteBuf.readShort();
                LOGGER.debug("header1={}", header1);

                // toString方法根据readerIndex最新值开始解码
                return byteBuf.toString(StandardCharsets.UTF_8);
            }
        }

        LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
        EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);

        byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeInt(bytes.length);
        byteBuf.writeShort(99);
        byteBuf.writeBytes(bytes);

        channel.writeInbound(byteBuf);
    }

    /**
     * lengthFieldOffset   = 1 (= the length of HDR1)
     * lengthFieldLength   = 4
     * lengthAdjustment    = 1 (= the length of HDR2)
     * initialBytesToStrip = 5 (= the length of HDR1 + LEN)
     * BEFORE DECODE (18 bytes)                       AFTER DECODE (13 bytes)
     * +------+--------+------+----------------+      +------+----------------+
     * | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
     * | 0xCA | 0x000C | 0xFE | "Hello, World" |      | 0xFE | "Hello, World" |
     * +------+--------+------+----------------+      +------+----------------+
     */
    @Test
    public void test05() {
        class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
            public LengthFieldDecoder() {
                super(1024, 1, 4, 1, 5);
            }

            @Override
            protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
                LOGGER.debug("长度读取前readerIndex={}", byteBuf.readerIndex());

                // 读取header2
                short header2 = byteBuf.readByte();
                LOGGER.debug("header2={}", header2);

                // toString方法根据readerIndex最新值开始解码
                return byteBuf.toString(StandardCharsets.UTF_8);
            }
        }

        LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
        EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);

        byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeByte(1);
        byteBuf.writeInt(bytes.length);
        byteBuf.writeByte(9);
        byteBuf.writeBytes(bytes);

        channel.writeInbound(byteBuf);
    }


    /**
     * lengthFieldOffset   =  1
     * lengthFieldLength   =  4 (整个消息的长度 = HDR1 + Length + HDR2 + Centent)
     * lengthAdjustment    = -5 (HDR1 + LEN的负值)
     * initialBytesToStrip =  5
     * BEFORE DECODE (18 bytes)                       AFTER DECODE (13 bytes)
     * +------+--------+------+----------------+      +------+----------------+
     * | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
     * | 0xCA | 0x0010 | 0xFE | "Hello, World" |      | 0xFE | "Hello, World" |
     * +------+--------+------+----------------+      +------+----------------+
     */
    @Test
    public void test06() {
        class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
            public LengthFieldDecoder() {
                super(1024, 1, 4, -5, 5);
            }

            @Override
            protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
                ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
                LOGGER.debug("长度读取前readerIndex={}", byteBuf.readerIndex());

                // 读取header2
                short header2 = byteBuf.readByte();
                LOGGER.debug("header2={}", header2);

                // toString方法根据readerIndex最新值开始解码
                return byteBuf.toString(StandardCharsets.UTF_8);
            }
        }

        LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
        EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);

        byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeByte(1);
        // 6=lengthField的长度+HEADER1的长度+HEADER2的长度
        byteBuf.writeInt(bytes.length + 6);
        byteBuf.writeByte(2);
        byteBuf.writeBytes(bytes);

        channel.writeInbound(byteBuf);
    }
}

源码解读-LengthFieldBasedFrameDecoder

decode方法

protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        // 校验是否丢弃超过最大数据帧长的字节
        if (discardingTooLongFrame) {
            discardingTooLongFrame(in);
        }

        // 可读字节长度小于lengthField结束的位置
        if (in.readableBytes() < lengthFieldEndOffset) {
            return null;
        }

        // 计算lengthField实际开始位置:以当前可读指针位置+lengthField偏移量为准
        int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset;

        // 获取数据帧长度
        long frameLength = getUnadjustedFrameLength(in, actualLengthFieldOffset, lengthFieldLength, byteOrder);

        if (frameLength < 0) {
            failOnNegativeLengthField(in, frameLength, lengthFieldEndOffset);
        }

        // 数据帧长度:需要减去lengthField之前的字节长度
        // 如果lengthFieldLength包含了header和所有字节数据,则需要通过lengthAdjustment减去lengthField之前的字节长度和lengthField本身
        frameLength += lengthAdjustment + lengthFieldEndOffset;

        if (frameLength < lengthFieldEndOffset) {
            failOnFrameLengthLessThanLengthFieldEndOffset(in, frameLength, lengthFieldEndOffset);
        }

        // 数据帧长度大于最大限制
        if (frameLength > maxFrameLength) {
            exceededFrameLength(in, frameLength);
            return null;
        }

        // 因为maxFrameLength是int类型,实际数据长度肯定不会超过int最大值
        int frameLengthInt = (int) frameLength;

        // 可读字节长度小于数据帧长度则直接返回null
        if (in.readableBytes() < frameLengthInt) {
            return null;
        }

        // 略去字节大于数据帧字节长度
        if (initialBytesToStrip > frameLengthInt) {
            failOnFrameLengthLessThanInitialBytesToStrip(in, frameLength, initialBytesToStrip);
        }

        // 更新readerIndex:readerIndex = readerIndex + initialBytesToStrip
        in.skipBytes(initialBytesToStrip);

        // 重组数据
        // 1-读取当前最新readerIndex
        int readerIndex = in.readerIndex();

        // 2-数据帧真实长度需要减去略过initialBytesToStrip
        int actualFrameLength = frameLengthInt - initialBytesToStrip;

        // 3-新建ByteBuf,解码后的数据字节
        ByteBuf frame = extractFrame(ctx, in, readerIndex, actualFrameLength);

        // 4-原先ByteBuf不可读
        in.readerIndex(readerIndex + actualFrameLength);
        return frame;
    }

getUnadjustedFrameLength方法

/**
 * length==lengthFieldLength
 * offset如果设置不对会和真实数据相差甚大
 * 比如:
 * ByteBuf byteBuf = Unpooled.buffer();
 * byteBuf.writeInt(1);
 * byteBuf.getUnsignedInt(0)可以得到正确的数据:0  (00000000 00000000 00000000 00000001)
 * byteBuf.getUnsignedInt(1)则会得到错误的数据:256(00000000 00000000 00000001 00000000)
 */
protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
        buf = buf.order(order);
        long frameLength;
        switch (length) {
        case 1:
            frameLength = buf.getUnsignedByte(offset);
            break;
        case 2:
            frameLength = buf.getUnsignedShort(offset);
            break;
        case 3:
            frameLength = buf.getUnsignedMedium(offset);
            break;
        case 4:
            frameLength = buf.getUnsignedInt(offset);
            break;
        case 8:
            frameLength = buf.getLong(offset);
            break;
        default:
            throw new DecoderException(
                    "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");
        }
        return frameLength;
    }

extractFrame方法

    protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
        // 截取得到解码后的数据内容
        return buffer.retainedSlice(index, length);
    }

到了这里,关于Netty-LengthFieldBasedFrameDecoder-解决拆包粘包问题的解码器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Netty是如何解决JDK中的Selector的bug的?

    例如臭名昭著的epoll bug,它会导致Selector空轮询,最终导致CPU 100%, 官方声称在JDK 1.6版本的update18修复了该问题,但是直到JDK1.7版本该问题仍旧存在,只不过该BUG发生 概率降低了一些而已,它并没有被根本解决,甚至JDK1.8的131版本中仍然存在 https://bugs.java.com/bugdatabase/view_bug.

    2024年02月20日
    浏览(69)
  • 长连接Netty服务内存泄漏,看我如何一步步捉“虫”解决

    作者:京东科技 王长春 事情要回顾到双11.11备战前夕,在那个风雨交加的夜晚,一个急促的咚咚报警,惊破了电闪雷鸣的黑夜,将沉浸在梦香,熟睡的我惊醒。 一看手机咚咚报警,不好!有大事发生了!电话马上打给老板: 老板说: 长连接吗? 我说:是的! 老板说:该来的

    2023年04月23日
    浏览(44)
  • 解决Redis连接问题:Caused by: io.netty.channel的异常

    在使用Redis时,有时候会遇到连接问题,其中一个常见的异常是Caused by: io.netty.channel异常。这个异常通常意味着与Redis服务器之间的网络通信发生了问题。本篇博客将深入讨论这个异常的可能原因和解决方法。 Caused by: io.netty.channel异常通常是由于以下原因引起的: 网络问题:

    2024年02月11日
    浏览(25)
  • Unable to load io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider解决

    出现这个错是因为项目使用到了网关之类的,我的是getaway模块路由转发报错 网上百度一大堆,但是与我的好像都不符合,因为我这个不是第一次就不行的,是之前可以,所以大概率不是他们所说的依赖问题。 解决方法:换个网络,这里可以直接选择自己的热点试下

    2024年02月10日
    浏览(30)
  • 什么?MC服务器又遇到了io???!!!(io.netty.channel.AbstractChannel问题分析与解决)

    当我正在期待地打开我自己搭建的MC服务器时 一点~!!! 几十秒钟的等待后,出现了IO!当然这里并不是IO接口,而是我对他的简称: io.netty.channel.AbstractChannel$AnnotatedConnectException:Connection timed out:no further information 这种错误我一看,我心都凉了,前面什么$什么io什么netty我都不管

    2024年02月05日
    浏览(64)
  • 成功解决:java.lang.NoSuchMethodError: reactor.netty.http.client.HttpClient.chunkedTransfer(Z)Lreactor/ne

    前言 在微服务中整合gateway网关,网关服务成功启动、在访问地址的时候报错。主要原因是依赖父工程 spring-boot-starter-parent 的版本和依赖网关 spring-cloud-starter-gateway 的版本不同导致。 在进行地址跳转的时候,没有做出相应的页面跳转。同时控制台报错 先前的版本(错误版本

    2024年02月16日
    浏览(74)
  • 解决 java sdk 链接的 fisco bcos报错的终极指南Caused by: io.netty.channel.ChannelException: init channel network

    有好友询问了一个关于fisco bcos java sdk 链接的问题,记录一下,有遇到的朋友可以参考解决!

    2024年02月11日
    浏览(32)
  • 【Netty】Netty 解码器(十二)

    回顾Netty系列文章: Netty 概述(一) Netty 架构设计(二) Netty Channel 概述(三) Netty ChannelHandler(四) ChannelPipeline源码分析(五) 字节缓冲区 ByteBuf (六)(上) 字节缓冲区 ByteBuf(七)(下) Netty 如何实现零拷贝(八) Netty 程序引导类(九) Reactor 模型(十) 工作原理

    2024年02月07日
    浏览(45)
  • [Netty] Netty自带的心跳机制 (十五)

    1.IdleStateHandler介绍 Netty服务端心跳机制: IdleStateHandler, 这个类可以对三种类型的心跳检测。 IdleHandler继承了ChannelInboundHandlerAdapter, 载了userEventTriggered方法, 执行了关闭连接的逻辑 2.IdleStateHandler源码解析 readerIdleTime:为读超时时间(即测试端一定时间内未接受到被测试端消息

    2023年04月24日
    浏览(28)
  • 【netty基础四】netty与nio

    阻塞I/O在调用InputStream.read()方法时是 阻塞的,它会一直等到数据到来 (或超时)时才会返回; 同样,在调用ServerSocket.accept()方法时,也会一直 阻塞到有客户端连接 才会返回,每个客户端连接成功后,服务端都会启动一个线程去处理该客户端的请求。 阻塞I/O的通信模型示意

    2024年02月10日
    浏览(77)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包