谢谢各位!
socket 编程中序列化和反序列化的作用 和例子 请举例说明!

解决方案 »

  1.   

    socket 发送的字节流,是数据
    1 如果你要发送字符串、文件、图片等流数据,序列化和反序列化对你没有作用
    2 如果你要传输对象,这时就要用到序列化和反序列化,怎样呢?
      如有个对象:
      //People类
      public class People{
       private int _ID = 0;
       public int ID{
         get{return _ID;}
         set{_ID = value;}
       }
       
      private string _name = 0;
       public string Name{
         get{return _name ;}
         set{_name = value;}
       }
     }
      
      People theGuy = new People();
      theGuy.ID = 1;
      theGuy.Name = "李小龙";那怎么把对象theGuy通过Socket发出去呢?
    step1: 把theGuy系列化,一般就是转换成字节流(如XML或string等)
    step2: 把转换成后字节流通过socket发出去
    step3: 接收端接收到发过来字节流后,把这些字节流转换成对象(就是反序列化)序列化 、反序列化涉及到反射、 XML等技术