Lv.的博客

C# Socket流数据大小端读写封装

 

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

封装代码如下:

 

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