C# Socket流数据大小端读写封装
网络数据是大端模式,而c#中的数据小端结构,那么在读写网络数据的时候需要进行转换。c#类库IPAddress已经封装了大小端的转换。
封装代码如下:
- using System.IO;
- using System.Net;
- using System;
- namespace Framework
- {
- public class NetStream
- {
- private MemoryStream stream;
- private BinaryReader reader;
- private BinaryWriter writer;
- public NetStream(byte[] buffer = null)
- {
- if (buffer == null)
- {
- this.stream = new MemoryStream();
- }
- else
- {
- this.stream = new MemoryStream(buffer);
- }
- this.reader = new BinaryReader(this.stream);
- this.writer = new BinaryWriter(this.stream);
- }
- public void Close()
- {
- this.stream.Close();
- this.reader.Close();
- this.writer.Close();
- }
- public long ReadInt64()
- {
- return IPAddress.HostToNetworkOrder(this.reader.ReadInt64());
- }
- public int ReadInt32()
- {
- return IPAddress.HostToNetworkOrder(this.reader.ReadInt32());
- }
- public int ReadInt16()
- {
- return IPAddress.HostToNetworkOrder(this.reader.ReadInt16());
- }
- public byte ReadByte()
- {
- return this.reader.ReadByte();
- }
- public string ReadString8()
- {
- return System.Text.Encoding.UTF8.GetString
- (
- this.reader.ReadBytes(ReadByte())
- );
- }
- public string ReadString16()
- {
- return System.Text.Encoding.UTF8.GetString
- (
- this.reader.ReadBytes(ReadInt16())
- );
- }
- public long Seek(long offset)
- {
- return this.stream.Seek(offset, SeekOrigin.Begin);
- }
- // -------------------------------------------------------------------------------
- public void WriteByte(byte value)
- {
- this.writer.Write(value);
- }
- public void WriteInt16(short value)
- {
- this.writer.Write
- (
- BitConverter.GetBytes
- (
- IPAddress.HostToNetworkOrder(value)
- )
- );
- }
- public void WriteInt32(int value)
- {
- this.writer.Write
- (
- BitConverter.GetBytes
- (
- IPAddress.HostToNetworkOrder(value)
- )
- );
- }
- public void WriteInt64(long value)
- {
- this.writer.Write
- (
- BitConverter.GetBytes
- (
- IPAddress.HostToNetworkOrder(value)
- )
- );
- }
- public void WriteString8(string value)
- {
- WriteByte
- (
- (byte) value.Length
- );
- this.writer.Write
- (
- System.Text.Encoding.UTF8.GetBytes(value)
- );
- }
- public void WriteString16(string value)
- {
- WriteInt16
- (
- (short) value.Length
- );
- this.writer.Write
- (
- System.Text.Encoding.UTF8.GetBytes(value)
- );
- }
- public byte[] GetBuffer()
- {
- return this.stream.ToArray();
- }
- public int GetLength()
- {
- return (int) this.stream.Length;
- }
- }
- }
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2015-04-27 boost库asio详解1——strand与io_service区别
2015-04-27 Timer.5 - Synchronising handlers in multithreaded programs
2015-04-27 Timer.4 - Using a member function as a handler
2015-04-27 Timer.3 - Binding arguments to a handler
2015-04-27 MFC定时器
2015-04-27 boost.asio系列——Timer
2015-04-27 boost 定时器.