C # Socket stream data size end read/write encapsulation

Source: Internet
Author: User

Network data is a big-endian mode, and in C # data in the small end of the structure, then read and write network data need to be converted. C # class Library IPAddress has encapsulated the conversion of the size end.

The encapsulation code is as follows:

[CSharp]View PlainCopy
  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 (shortvalue)
  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 stream data size end read/write encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.