网络数据是大端模式,而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. }

最新文章

  1. JSX语法简介
  2. java 启动 shell脚本
  3. Eratosthenes筛选法构造1-n 素数表
  4. windows环境下,如何启动chromedriver
  5. wflag
  6. VS2008注册码
  7. SQL SERVER 中 GO 的用法2
  8. wpf viewmodel之间的通信
  9. Delphi SysErrorMessage 函数和系统错误信息表
  10. SecureCRT退出全屏方法
  11. LNA
  12. Go基础(1)
  13. 总结:BGP和静态路由并存,达到故障自动倒换的目的。
  14. 用kali执行arp攻击-----------使对方断网
  15. 【LeetCode】163. Missing Range
  16. 使用scrollTop返回顶部
  17. python的数字图像处理学习(2)
  18. Jquery 获取屏幕及滑块及元素的高度及距离
  19. 轻量级IOC框架:Ninject (上)
  20. 【Centos】【Python】【Flask】阿里云上部署一个 flask 项目

热门文章

  1. 用python打造简单的cms识别
  2. Python学习day43-数据库(多表关系)
  3. python 之 字符串处理
  4. C++和C#之间的数据类型对应关系
  5. 玩转大数据之Apache Pig如何与Apache Lucene集成
  6. C++面向对象高级编程(下)第二周-Geekband
  7. 构建工具Bazel入门
  8. 深喉起底APP线下预装市场,如何一夜间拥有千万用户
  9. MySQL数据库_索引_事务_优化 _锁_存储引擎_存储过程_CAP
  10. 解决hive无法传参问题思路