0

I am having an issue which i am not sure how to solve efficiently. I made a server which can handle multiple clients through TCP sockets. These clients send data (multiple different objects!) to the server. The server stores the objects, and then re-sends it to all the interested clients.

Now, the client receives a Binary Serialized ''object'' and it has no clue whatsoever, what class this object is made from. A way to find out, is to just deseralize with all possible classes that i know it would send as a client. and check if it succeeded or not. However, one can imagine that when i have like 50 different classes to deserialize with later on, this would be VERY inefficient when i get a lot of messages over my socket.

Serialization happens in this code, which i run by a thread and currently only sends dummy data:

    public static void NotifyService()
    {
        Stream stream = client.GetStream();
        IFormatter formatter = new BinaryFormatter();
        while (true)
        {
            try
            {
                Class1 klas = new Class1();
                Class2 klasen = new Class2();
                formatter.Serialize(stream, klas);
                formatter.Serialize(stream, klasen);
            }
            catch (Exception e)
            {
                Console.WriteLine("Notifier got Exception, closing notify thread: " + e);
                break;
            }

        }
        stream.Close();
    }

The question is now: What would be the most efficient way for the client to deserialize my object properly?

(There are other ways to do the sending an receiving stuff as is, using XML and other stuff, but i wanted to do objects with Binary serialization) Keep in mind that i am very new to the sockets/serialization in general.

2
  • what code are you using for the binary serialization? Commented Jul 9, 2013 at 15:08
  • Added code used for serialization. Commented Jul 9, 2013 at 15:13

1 Answer 1

1

Since you are using the binary formatter, the most efficient way to deserialize your data is to use the Deserialize method of the binary formatter.

Also, when you do your deserialization, if you want to know of which type your object is, you can just use this code:

BinaryFormatter bf;
object obj =  bf.Deserialize(some_stream);
obj.GetType().Name;  // here you have the name of the class you just deserialized
Sign up to request clarification or add additional context in comments.

4 Comments

It really is as simple as that? Cool! I am going to try this right away, thanks!
If i Deserialize, but keep at an object ( never cast it to what it was when i serialized it) do i still need the original class available to be able to do the deserialize?
what do you mean by "available"? Do you mean "Do I need to load the assembly that contains that class?"
I already got it answered, it seems that to let my server make an object out of it, and let it send that object to other clients, my server either needs to know what class type it is etc. or i keep it binary, and i need to know the length of the package i am receiving on the server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.