Netty学习笔记八、 Sacttering和Gathering代码演示

package com.hao.demo.netty.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

/**
 * @author
 * @date 2020-05-13
 */
public class SactteringAndGatheringTest {

    /**
     * 1、Sacttering:将数据写入到buffer时,可以采用buffer数组 依次写入 分散
     * 2、Gathering:采用buffer读取数据时,可以采用buffer数组 依次读
     * 3、 测试的话 telent 127.0.0.1 7000
     * @param args
     */
    public static void main(String[] args) throws Exception {

        // 使用 ServerSocketChannel 和 SocketChannel 网络
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress(7000);

        // 绑定端口 并启动
        serverSocketChannel.socket().bind(inetSocketAddress);

        // 创建buffer数组
        ByteBuffer[] byteBuffers = new ByteBuffer[2];

        byteBuffers[0] = ByteBuffer.allocate(5);
        byteBuffers[1] = ByteBuffer.allocate(3);

        // 等待客户端连接
        SocketChannel socketChannel = serverSocketChannel.accept();

        int messageLength = 8; // 假定从客户端接收8个字节

        // 循环读取
        while (true) {

            int byteRead = 0;
            while (byteRead < 8) {
                long l = socketChannel.read(byteBuffers);
                System.out.println("byteRead=" + byteRead);
                byteRead += 1; // 累计读取的字节

                // 使用流打印, 看看当前的这个buffer的position 和 limit
                Arrays.asList(byteBuffers).stream().map(byteBuffer -> "position =" +
                        byteBuffer.position() + ", limit =" + byteBuffer.limit()).forEach(System.out::println);

            }

            // 将所有的buffer 进行反转
            Arrays.asList(byteBuffers).forEach(ByteBuffer::flip);

            long byteWrite = 0;
            while (byteWrite < messageLength) {
                long l = socketChannel.write(byteBuffers);// 回送
                byteWrite += 1; // 累计写入的字节
            }

            // 将所有的buffer 复位 clear
            Arrays.asList(byteBuffers).forEach(ByteBuffer::clear);

            System.out.println("byteRead: = "+ byteRead + "byteWriter: =" +byteWrite
            + "messageLength =" + messageLength);
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值