C# Socket流資料大小端讀寫封裝

來源:互聯網
上載者:User

標籤:

 

網路資料是大端模式,而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. }  

C# Socket流資料大小端讀寫封裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.