日期:2014-05-18  浏览次数:20934 次

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

------解决方案--------------------
[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;
}