C#.网络编程 Socket基础(十) List<byte>队列缓存Socket接收的数据:List<byte>插入byte[]数组;反之List<byte>可拷贝到byte[]数组

一、简介

     缓存Socket发送的数据,放到队列中,然后固定地从队列总读取字节。

关键点1:List<byte>列表

List<byte> VideoList = new List<byte>();

List数组可以实现快速地增删。如果用byte[] VideoList = new byte[1000],来缓存,那么久需要进行移位等等操作,增加了复杂度。

List数组本身移位的功能、插入的动能。

关键点2:VideoList 不但可以插入单个数据,还可以插入 byte[]字节数组 

  byte[] byteD = new byte[4] { 4, 4, 4, 4 };
            byte[] byteB = new byte[2] { 2, 2 };

            //插入:一个一个字节插入
            VideoList.Add(2);
            VideoList.Add(3);
            VideoList.Add(10);
            VideoList.Add(32);
            VideoList.Add(12);

            //插入:将字节数组插入到末尾
            VideoList.AddRange(byteD);
            VideoList.AddRange(btVedioRecByte);

关键点3:List<byte>列表类型的内容可以拷贝到byte[]字节数组中

     //拷贝
            byte[] btBitmapImage = new byte[IMAGE_LENGHT];
            Array.Copy(VideoList.ToArray(), btBitmapImage, IMAGE_LENGHT); //将List<byte>的内容拷贝到byte[]:VideoList.ToArray()

二、Codes

#region List<byte> 列表 队列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QueueSample
{
    class Program
    {
        static List<byte> VideoList = new List<byte>();
       
        const int IMAGE_LENGHT = 3;
        byte[] btBitmapImage = new byte[IMAGE_LENGHT];
        static void Main(string[] args)
        {
            byte[] byteC = new byte[3] { 3, 3,3};
            VideoQuene(byteC);
        }

        private static byte[] VideoQuene(byte[] btVedioRecByte)
        {
            byte[] byteD = new byte[4] { 4, 4, 4, 4 };
            byte[] byteB = new byte[2] { 2, 2 };

            //插入:一个一个字节插入
            VideoList.Add(2);
            VideoList.Add(3);
            VideoList.Add(10);
            VideoList.Add(32);
            VideoList.Add(12);

            //插入:将字节数组插入到末尾
            VideoList.AddRange(byteD);
            VideoList.AddRange(btVedioRecByte);

            //拷贝
            byte[] btBitmapImage = new byte[IMAGE_LENGHT];
            Array.Copy(VideoList.ToArray(), btBitmapImage, IMAGE_LENGHT); //将List<byte>的内容拷贝到byte[]:VideoList.ToArray()

            //删除
            VideoList.RemoveRange(0, IMAGE_LENGHT);
            return btBitmapImage;
        }
    }
}
#endregion

 

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值