C#处理网络传输中不完整的数据流

1、背景

在读取byte数组的场景(例如:读取文件、网络传输数据)中,特别是网络传输的场景中,非常有可能接收了不完整的byte数组,在将byte数组转换时,因字符的缺失/增多,转为乱码。如下图所示

2、具体实现

在C#中有Decoder可供使用,其内部有换存(buffer),可以现将非完整的byte存储起来,等到后新的数据后,再拼接起来,形成完成的内容。具体代码及说明如下:

csharp 复制代码
// See https://aka.ms/new-console-template for more information
using System.ComponentModel;
using System.Text;

//初始化的字符串
string helloStr = "你好Ha";
Console.WriteLine(helloStr);

byte[] helloArray=Encoding.UTF8.GetBytes(helloStr);
Console.WriteLine("你好Ha对应的byte数组内容(十六进制):" + BitConverter.ToString(helloArray));


ArraySegment<byte> helloSegment= new ArraySegment<byte>(helloArray,0,4);
Console.WriteLine("若网络传输中先收到了前4个:"+BitConverter.ToString(helloSegment.ToArray()));
Console.WriteLine("转换为string会发现结果是不对的:"+Encoding.UTF8.GetString(helloSegment));

Console.WriteLine("下面是使用GetDecoder方法进行处理");
Decoder dncoder = Encoding.UTF8.GetDecoder();

int charCount= dncoder.GetCharCount(helloSegment, false);
Console.WriteLine("还是同样的前4个,判断收到了几个【完整】的字符:"+charCount);

Console.WriteLine("将前3位,加载到halfCharArray数组中");
char[] halfCharArray = new char[charCount];
dncoder.GetChars(helloSegment.ToArray(), 0, helloSegment.Count, halfCharArray, 0);

Console.WriteLine("解析完的字符是:"+ new string(halfCharArray));


ArraySegment<byte> otherHalfStr = new ArraySegment<byte>(helloArray, 4, 4);
Console.WriteLine("若网络传输中先收到了后4个:" + BitConverter.ToString(otherHalfStr.ToArray()));

//继续转换
//若能判断,本次接受的数据为最终的数据,则GetCharCount的第二个参数,应当设置为true,(强制清空Buffer,避免后续使用时影响后续的解析)
int otherCharCount = dncoder.GetCharCount(otherHalfStr, true);  
Console.WriteLine("缓存+新接受的字符数量:" + otherCharCount);
char[] ohterHalfCharArray = new char[otherCharCount];
dncoder.GetChars(otherHalfStr.ToArray(), 0, otherHalfStr.Count, ohterHalfCharArray, 0);
Console.WriteLine("解析完的字符是:" + new string(ohterHalfCharArray));

Console.Read();

以上代码运行后的结果,如图所示:

3、参考

官网的参考资料

相关推荐
从孑开始6 小时前
ManySpeech.MoonshineAsr 使用指南
人工智能·ai·c#·.net·私有化部署·语音识别·onnx·asr·moonshine
YuanlongWang6 小时前
C# 中,依赖注入(DI)的实现方式
c#
SmartSoftHelp开发辅助优化8 小时前
C# WinForm 编程高手:程序,进程,线程。程序,窗体,UI,后台。是如何协调工作的?深度解析>SmartSoftHelp魔法精灵工作室
microsoft·ui·c#
future_studio10 小时前
聊聊 Unity(小白专享、C# 小程序 之 加密存储)
开发语言·小程序·c#
c#上位机11 小时前
MefBootstrapper在Prism引导程序中的使用
c#·wpf·prism
玩泥巴的13 小时前
.NET驾驭Word之力:基于规则自动生成及排版Word文档
c#·word·.net·com互操作
SunnyDays101114 小时前
C# 实现高保真 Excel 转 PDF(无需 Office 环境)
经验分享·c#·excel转pdf
攻城狮CSU14 小时前
C# 数据加载专题 之泛型序列化
java·servlet·c#
爱编程的鱼14 小时前
C# 参数详解:从基础传参到高级应用
开发语言·microsoft·c#
流水线上的指令侠16 小时前
使用C#写微信小程序后端——电商微信小程序
微信小程序·小程序·c#·visual studio