C#如何使用socket发送序列化对象

来源:百度知道 编辑:UC知道 时间:2024/09/27 06:26:19

// 用来序列化对象的类
public class Serialize
{
public static byte[] serialize(object obj)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, obj);
byte[] tmp = ms.ToArray();
ms.Close();
return tmp;
}
}
byte[] sends = Serialize.serialize(obj);
socket.send(sends);