请问如何用 SOCKET 传送结构,我用序列化试过,不行。请提供代码。谢谢。

解决方案 »

  1.   

    [Serializable()]
    public class Message
    {
    private string _Heard="";
    private string _Text="";
    private string _Tag=""; public Message()
    { } public string Heard
    {
    get
    {
    return _Heard;
    }
    set
    {
    _Heard=value;
    }
    } public string Text
    {
    get
    {
    return _Text;
    }
    set
    {
    _Text=value;
    }
    } public string Tag
    {
    get
    {
    return _Tag;
    }
    set
    {
    _Tag=value;
    }
    }
            //反序列化
    public Message DeSerialize(byte[] bytes )
    {
    System.Runtime.Serialization.IFormatter format=null;
    format=new 
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    System.IO.MemoryStream stream=null;
    stream=new System.IO.MemoryStream();
    stream.Seek(0,System.IO.SeekOrigin.Begin);
    stream.Write(bytes,0,bytes.Length);
    stream.Seek(0,System.IO.SeekOrigin.Begin);
    Message msg=(Message)format.Deserialize(stream);
                return msg;
    } }
    这是序列化private byte[] Serialize(MSG TYMsg)   //序列化
    {
    format=new 
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    stream=new System.IO.MemoryStream();
    format.Serialize(stream,TYMsg);
    stream.Seek(0,System.IO.SeekOrigin.Begin);
    byte[] bytes=stream.ToArray();
                return bytes;
    }
      

  2.   

    谢谢,代码可行。
    但我想问一下我的为什么不行?
    我也是和你差不多,只是  System.Runtime.Serialization.IFormatter format=null 这里我是这样定义的  System.Runtime.Serialization.Formatters.BinaryFormatter format;
    它老是说找不到程序集。
      

  3.   

    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter format;
    而不是System.Runtime.Serialization.Formatters.BinaryFormatter format;