views:

230

answers:

2

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.

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;
    }




}
A: 

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.

empi
+1  A: 

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.

Krisc
Krisc is right. You need to do "message framing" by sending the size of the packet upfront. Then you can issue a read for the correct #of bytes and then deserialize that packet.For an example, see http://ferozedaud.blogspot.com/2009/11/how-to-send-object-from-java-to-net.html. You can ignore the java part, the .NET part will show you how to do message framing.
feroze