反序列化从服务器发送到客户端的对象时遇到麻烦 [英] Having trouble deserializing objects sent from server to client
问题描述
问候.我正在使用System.Net在XNA游戏中实现客户端/服务器多人游戏系统.我已经有了游戏,可以成功地在客户端和服务器之间来回传递对象,但是我发现客户端会随机地
错误并显示以下错误:
二进制流"0"不包含有效的BinaryHeader.可能的原因是序列化和反序列化之间无效的流或对象版本更改.
Greetings. I'm working on implementing a client/server multiplayer system in an XNA game using System.Net. I've gotten the game to successfully pass objects back and forth between client and server, but I've found that the client will randomly
error out with the following error:
Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
我已经玩了好几个小时了,但无法弄清楚为什么有时可以反序列化对象,并在其他时间给出此错误.我在下面粘贴了我的代码段.让我知道您是否需要查看更多内容.谢谢 提前寻求帮助.
I've been playing around with this for hours, but cannot figure out why it is able to sometimes deserialize the objects, and gives this error other times. I have pasted snippets of my code below. Let me know if you need to see more. Thanks in advance for the help.
注意:对于代码的格式,我深表歉意.每次按保存时,每个空格都会变成换行符.如果我做错了,请告诉我.
Note: I apologize for the formatting of the code. Everytime I press save, every space gets turned into a line break. If I am doing something wrong, please let me know.
客户:
public
static
void
SendToServer(object
obj)
{
Client.Send(obj);
}
public
static
object
ReceiveFromServer()
{
return
Client.ReceiveObj();
}
public
static
object
ReceiveObj()
{
//if connected to the server
if
(!Network.ServerDiscon)
{
//declare string and byte[] vars
object
obj = null
;
byte
[] length = new
byte
[4];
client.Client.Receive(length, 0, 4, SocketFlags.None);
int
size = BitConverter.ToInt32(length, 0);
//convert string to int
byte
[] bytes = new
byte
[size];
client.Client.Receive(bytes, 0, size, SocketFlags.None);
Stream memStream = new
MemoryStream(bytes);
BinaryFormatter formatter = new
BinaryFormatter();
Console.WriteLine("stream length: "
+ memStream.Length.ToString());
obj = formatter.Deserialize(memStream);
memStream.Close();
return
obj;
}
else
return
null
;
}
public
static
void
Send(object
obj)
{
//initiate stream and formatter
Stream stream = new
MemoryStream();
BinaryFormatter formatter = new
BinaryFormatter();
//serialize the object
formatter.Serialize(stream, obj);
stream.Position = 0;
//convert stream into bytes array
byte
[] bytes = ((MemoryStream)stream).ToArray();
byte
[] length = BitConverter.GetBytes(bytes.Length);
stream.Flush();
stream.Position = 0;
//try to write output to stream. if failed, server has disconnected
client.Client.Send(length, 0, 4, SocketFlags.None);
client.Client.Send(bytes, 0, bytes.Length, SocketFlags.None);
stream.Close();
}
推荐答案
您是否考虑过将净流直接传递给二进制格式化程序?
Have you considered directly passing the net stream to the binary formatter?
我正在猜测您的客户"对象派生自TcpClient对象?如果是这样,则可以将TcpClient.GetStream()直接传递到二进制格式化程序中,-使其 读取流.
I am making a guess that your "client" object derives from the TcpClient object? if it does, you can pass TcpClient.GetStream() directly into the binary formatter, - let it read through the stream.
public static object DeSerializeNetObject(TcpClient client)
{
// create the binary formatter:
var fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// deserialize
return fmt.Deserialize(client.GetStream());
}
这篇关于反序列化从服务器发送到客户端的对象时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!