I've written some code which is a mini simple imitation of messenger program. In the program; when the user signs out, instance of my LogOutCommand class is prepared by client program, serialized, and sent to server. When the server receives the LogOutCommand, it deserializes and invokes Execute method of the class, which performs db operations, etc.
The problem is that, sometimes Server can deserialize very well, but sometimes fails. As far as I understand, server sometimes starts deserialization before associated bytes are sent totally and accurately.开发者_如何学Go
How can I make the server start deserialization in a way that it waits for all associated bytes are completed being sent?
Or do you think there is another problem?
Here is the code:
// Server listens to socket
private void ReadData(object obj)
{
Socket client = (Socket)obj;
while (true)
{
if (client.Available > 0)
{
byte[] buffer = new byte[client.Available];
client.Receive(buffer);
ServerCommandBase cmd = CommandReader.ReadSrvCommand(buffer);
cmd.Execute(context);
}
}
}
//CommandReader class
public class CommandReader
{
public static ServerCommandBase ReadSrvCommand(byte[] buffer)
{
return (ServerCommandBase)SerializationUtility.SerializationHelper.Deserialize(buffer);
}
public static ClientCommandBase ReadCliCommand(byte[] buffer)
{
return (ClientCommandBase)SerializationUtility.SerializationHelper.Deserialize(buffer);
}
}
// Serialization / Deserialization class
public class SerializationHelper { public static byte[] Serialize(object obj) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); try { formatter.Serialize(stream, obj); } catch (Exception) { MessageBox.Show("Serialize Edilemiyor"); }
stream.Position = 0; return stream.ToArray(); } public static object Deserialize(byte[] byteArr)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream(byteArr);
ms.Position = 0;
object retObj = null;
try
{
retObj = formatter.Deserialize(ms);
}
catch (Exception)
{
MessageBox.Show("Cannot Be Deserialized!");
}
return retObj;
}
}
The problem as I see it is that you are attempting to deserialize data as soon as it comes down the pipe. This may or may not be possible due to how the packets are sent across the network.
You should send a header with your message that instructs how long (in bytes) the data will be. You then take in the bytes until that number has been hit and deserialize then and only then.
Your header should be of a specific format and have a specific length so that it is easy to take it off at the beginning.
First of all you shouldn't use BinaryFormatter. It's much better to use e.g. XmlSerializer.
Secondly, please include exception details that you get.
精彩评论