当我尝试将某些内容加载到MemoryStream
时出现此错误。我尝试通过网络在服务器上发送图片(字节流)。但每当我尝试发送它时,它都会说
在处理完成之前已达到流结束。
带有错误的行标记在下面的代码中
namespace Model_Library
{
[Serializable]
public class Package
{
public List<object> DATA;
public int ID;
public PackageType packetType;
public Package(PackageType u_type, int u_ID)
{
DATA = new List<object>();
this.ID = u_ID;
this.packetType = u_type;
}
public Package(byte[] packetBytes)
{
using (MemoryStream ms = new MemoryStream(packetBytes))
{
BinaryFormatter bf = new BinaryFormatter();
Package p = (Package)bf.Deserialize(ms); //Here is the error
ID = p.ID;
DATA = p.DATA;
packetType = p.packetType;
}
}
public byte[] toBytes()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, this);
byte[] bytes = ms.ToArray();
ms.Close();
return bytes;
}
}
public enum PackageType
{
connect,
login,
registration,
friends,
message,
message_confirmation,
load_history,
search_friends,
add_friend
}
}
答案 0 :(得分:0)
确保所有数据都是通过网络收到的,你没有在这里显示,但是你使用的是NetworkStream.DataAvailable然后请阅读这个答案,因为它是关于如何管理阅读数据的一些细节离开网络 - TcpClient.GetStream().DataAvailable returns false, but stream has more data