zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    C#面向对象(二)之抽象类实现多态
    JavaWeb 学习0010-今日问题 2016-12-3
    JavaWeb 学习008-今日问题(非空验证尚未解决) 2016-12-2
    JavaWeb 学习007-4个页面,5条sql语句(添加、查看、修改、删除)2016-12-2
    JavaWeb 学习006-4个页面,5条sql语句(添加、查看、修改、删除)
    JavaWeb 学习005-4个页面,5条sql语句(添加、查看、修改、删除)
    JavaWeb 学习004-增删改查的编写
    JavaWeb 学习003-简单登录页面功能实现
    JavaWeb 学习001-登录页面-Servlet
    JavaWeb 学习001-登录页面
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/5440521.html
Copyright © 2011-2022 走看看