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# Socket流数据大小端读写封装的更多相关文章
- C++/java之间的Socket通信大小端注意事项
在一个物联往项目中,需要java云平台与一个客户端做socket定制协议的通信:然而在第一次测试时,并没有按照预想的那样完成解析.查找资料以后是因为客户端的数据读取方式为小端模式,而java默认采用大 ...
- 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据
[源码下载] 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数 ...
- 【X86】---X86处理器大小端的数据存储验证
之前也关注过大小端的存储,可能时间久了,加之又之前的电脑抽象换成了当前的处理器寄存器的值判断,导致自己总是有点蒙圈.看Spec手册的时候,有时会无法与手册中某个Bit的值与RU/RW工具读出来的对应上 ...
- C语言随笔3:指针定义、数据在地址中的大小端排列
指针变量:用于存放另一个变量的地址 (指针变量所占空间大小由操作系统决定32/64位 4/8字节 // 声明且定义:int *p=&a: 声明.定义:int *p: p= &a: ...
- readLine读取socket流的时候产生了阻塞
BufferedReader的readLine方法,只要读到流结束或者流关闭,就会返回null 在读取文件的时候,文件结尾就是流的结尾,但对于Socket而言不是的.不能认为流中数据读完了就是流的结尾 ...
- 脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?
1.引言 本文接上篇<脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手>,继续脑残式的网络编程知识学习 ^_^. 套接字socket是大多数程序员都非常熟悉的概念,它是计算机 ...
- [转帖]脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?
脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么? http://www.52im.net/thread-1732-1-1.html 1.引言 本文接上篇<脑残式网 ...
- Apache RocketMQ分布式消息传递和流数据平台及大厂面试宝典v4.9.2
概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache RocketMQ官网地址 https://rocketmq.apache.org/ Latest rel ...
- C语言 - 大小端问题
目前使用的机器都是使用字节BYTE来存储的. 对于跨越多字节的对象,必须搞清楚两个规则: 这个对象的地址是什么 在存储器中如何按照这些字节的存放的书序 对于一个整型对象 a=0x12345678,一共 ...
随机推荐
- Python使用微信接入图灵机器人
1.wxpy库介绍 wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展. 文档地址:https://wxpy.readthedocs.io 从 PYPI 官 ...
- 《DSP using MATLAB》Problem 8.29
来汉有一月,往日的高温由于最近几个台风沿海登陆影响,今天终于下雨了,凉爽了几个小时. 接着做题. %% ------------------------------------------------ ...
- Python学习day37-并发编程(3)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- MyEclipse设置 web访问根路径
使用鼠标右键点击项目(点击属性properties)进入如下图:
- 构建HBase二级索引
- Leetcode953. Verifying an Alien Dictionary验证外星语词典
某种外星语也使用英文小写字母,但可能顺序 order 不同.字母表的顺序(order)是一些小写字母的排列. 给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在 ...
- P1080(python 高精度)
https://www.luogu.org/problem/P1080 n=int(input()) s=input().split() S=int(s[0]) T=int(s[1]) a=[] fo ...
- centos yum 坏掉 db 损坏
#首先清除掉缓存,之后再重建就可以了 rm -f /var/lib/rpm/__db* rpm --rebuilddb 提示的错误应该是: error: rpmdb: BDB0113 Thread/p ...
- PAT甲级——A1042 Shuffling Machine
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- JasperReport查看和打印报告7
报表填充过程JasperPrint对象的输出可以使用内置的浏览器组件来查看,打印或导出到更多的流行的文件格式,如PDF,HTML,RTF,XLS,ODT,CSV或XML.Jasper文件查看和打印将包 ...