因此,我尝试使用以下代码通过网络流发送“数据包”类:
IFormatter formatter = new BinaryFormatter();
NetworkStream stream = client.GetStream();
formatter.Serialize(stream, packet);
stream.Flush();
stream.Close();
client.Close();
使用这个类:
[Serializable]
public class Packet
{
public string header;
public string content;
public int size = 0;
public Packet(string header, string content)
{
this.header = header;
this.content = content;
size = Encoding.ASCII.GetByteCount(header) + Encoding.ASCII.GetByteCount(content);
}
}
但是在另一边阅读时出现以下错误:
'System.Runtime.Serialization.SerializationException: Unable to find assembly 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'
这是我的阅读代码:
NetworkStream ns = client.GetStream();
IFormatter formatter = new BinaryFormatter();
Packet p = (Packet)formatter.Deserialize(ns);
MessageBox.Show(p.header);
return p;
知道为什么会这样吗?
编辑:
服务器端数据包类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Server
{
public class Packet
{
public string header;
public string content;
public int size = 0;
public Packet(string header, string content)
{
this.header = header;
this.content = content;
size = Encoding.ASCII.GetByteCount(header) + Encoding.ASCII.GetByteCount(content);
}
}
}