网络数据是大端模式,而c#中的数据小端结构,那么在读写网络数据的时候需要进行转换。c#类库IPAddress已经封装了大小端的转换。

封装代码如下:

  1. using System.IO;
  2. using System.Net;
  3. using System;
  4. namespace Framework
  5. {
  6. public class NetStream
  7. {
  8. private MemoryStream stream;
  9. private BinaryReader reader;
  10. private BinaryWriter writer;
  11. public NetStream(byte[] buffer = null)
  12. {
  13. if (buffer == null)
  14. {
  15. this.stream = new MemoryStream();
  16. }
  17. else
  18. {
  19. this.stream = new MemoryStream(buffer);
  20. }
  21. this.reader = new BinaryReader(this.stream);
  22. this.writer = new BinaryWriter(this.stream);
  23. }
  24. public void Close()
  25. {
  26. this.stream.Close();
  27. this.reader.Close();
  28. this.writer.Close();
  29. }
  30. public long ReadInt64()
  31. {
  32. return IPAddress.HostToNetworkOrder(this.reader.ReadInt64());
  33. }
  34. public int ReadInt32()
  35. {
  36. return IPAddress.HostToNetworkOrder(this.reader.ReadInt32());
  37. }
  38. public int ReadInt16()
  39. {
  40. return IPAddress.HostToNetworkOrder(this.reader.ReadInt16());
  41. }
  42. public byte ReadByte()
  43. {
  44. return this.reader.ReadByte();
  45. }
  46. public string ReadString8()
  47. {
  48. return System.Text.Encoding.UTF8.GetString
  49. (
  50. this.reader.ReadBytes(ReadByte())
  51. );
  52. }
  53. public string ReadString16()
  54. {
  55. return System.Text.Encoding.UTF8.GetString
  56. (
  57. this.reader.ReadBytes(ReadInt16())
  58. );
  59. }
  60. public long Seek(long offset)
  61. {
  62. return this.stream.Seek(offset, SeekOrigin.Begin);
  63. }
  64. // -------------------------------------------------------------------------------
  65. public void WriteByte(byte value)
  66. {
  67. this.writer.Write(value);
  68. }
  69. public void WriteInt16(short value)
  70. {
  71. this.writer.Write
  72. (
  73. BitConverter.GetBytes
  74. (
  75. IPAddress.HostToNetworkOrder(value)
  76. )
  77. );
  78. }
  79. public void WriteInt32(int value)
  80. {
  81. this.writer.Write
  82. (
  83. BitConverter.GetBytes
  84. (
  85. IPAddress.HostToNetworkOrder(value)
  86. )
  87. );
  88. }
  89. public void WriteInt64(long value)
  90. {
  91. this.writer.Write
  92. (
  93. BitConverter.GetBytes
  94. (
  95. IPAddress.HostToNetworkOrder(value)
  96. )
  97. );
  98. }
  99. public void WriteString8(string value)
  100. {
  101. WriteByte
  102. (
  103. (byte) value.Length
  104. );
  105. this.writer.Write
  106. (
  107. System.Text.Encoding.UTF8.GetBytes(value)
  108. );
  109. }
  110. public void WriteString16(string value)
  111. {
  112. WriteInt16
  113. (
  114. (short) value.Length
  115. );
  116. this.writer.Write
  117. (
  118. System.Text.Encoding.UTF8.GetBytes(value)
  119. );
  120. }
  121. public byte[] GetBuffer()
  122. {
  123. return this.stream.ToArray();
  124. }
  125. public int GetLength()
  126. {
  127. return (int) this.stream.Length;
  128. }
  129. }
  130. }

C# Socket流数据大小端读写封装的更多相关文章

  1. C++/java之间的Socket通信大小端注意事项

    在一个物联往项目中,需要java云平台与一个客户端做socket定制协议的通信:然而在第一次测试时,并没有按照预想的那样完成解析.查找资料以后是因为客户端的数据读取方式为小端模式,而java默认采用大 ...

  2. 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据

    [源码下载] 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数 ...

  3. 【X86】---X86处理器大小端的数据存储验证

    之前也关注过大小端的存储,可能时间久了,加之又之前的电脑抽象换成了当前的处理器寄存器的值判断,导致自己总是有点蒙圈.看Spec手册的时候,有时会无法与手册中某个Bit的值与RU/RW工具读出来的对应上 ...

  4. C语言随笔3:指针定义、数据在地址中的大小端排列

    指针变量:用于存放另一个变量的地址 (指针变量所占空间大小由操作系统决定32/64位  4/8字节 // 声明且定义:int  *p=&a: 声明.定义:int  *p: p= &a: ...

  5. readLine读取socket流的时候产生了阻塞

    BufferedReader的readLine方法,只要读到流结束或者流关闭,就会返回null 在读取文件的时候,文件结尾就是流的结尾,但对于Socket而言不是的.不能认为流中数据读完了就是流的结尾 ...

  6. 脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?

    1.引言 本文接上篇<脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手>,继续脑残式的网络编程知识学习 ^_^. 套接字socket是大多数程序员都非常熟悉的概念,它是计算机 ...

  7. [转帖]脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?

    脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?     http://www.52im.net/thread-1732-1-1.html   1.引言 本文接上篇<脑残式网 ...

  8. Apache RocketMQ分布式消息传递和流数据平台及大厂面试宝典v4.9.2

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache RocketMQ官网地址 https://rocketmq.apache.org/ Latest rel ...

  9. C语言 - 大小端问题

    目前使用的机器都是使用字节BYTE来存储的. 对于跨越多字节的对象,必须搞清楚两个规则: 这个对象的地址是什么 在存储器中如何按照这些字节的存放的书序 对于一个整型对象 a=0x12345678,一共 ...

随机推荐

  1. Python使用微信接入图灵机器人

    1.wxpy库介绍 wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展. 文档地址:https://wxpy.readthedocs.io 从 PYPI 官 ...

  2. 《DSP using MATLAB》Problem 8.29

    来汉有一月,往日的高温由于最近几个台风沿海登陆影响,今天终于下雨了,凉爽了几个小时. 接着做题. %% ------------------------------------------------ ...

  3. Python学习day37-并发编程(3)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. MyEclipse设置 web访问根路径

    使用鼠标右键点击项目(点击属性properties)进入如下图:

  5. 构建HBase二级索引

  6. Leetcode953. Verifying an Alien Dictionary验证外星语词典

    某种外星语也使用英文小写字母,但可能顺序 order 不同.字母表的顺序(order)是一些小写字母的排列. 给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在 ...

  7. P1080(python 高精度)

    https://www.luogu.org/problem/P1080 n=int(input()) s=input().split() S=int(s[0]) T=int(s[1]) a=[] fo ...

  8. centos yum 坏掉 db 损坏

    #首先清除掉缓存,之后再重建就可以了 rm -f /var/lib/rpm/__db* rpm --rebuilddb 提示的错误应该是: error: rpmdb: BDB0113 Thread/p ...

  9. PAT甲级——A1042 Shuffling Machine

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  10. JasperReport查看和打印报告7

    报表填充过程JasperPrint对象的输出可以使用内置的浏览器组件来查看,打印或导出到更多的流行的文件格式,如PDF,HTML,RTF,XLS,ODT,CSV或XML.Jasper文件查看和打印将包 ...