Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Im trying to send some object from a server to the client. My problem is that when im sending only 1 object, everything works correctly. But at the moment i add another object an exception is thrown - "binary stream does not contain a valid binaryheader" or "No map for object (random number)". My thoughts are that the deserialization does not understand where the stream starts / ends and i hoped that you guys can help me out here.

heres my deserialization code:

public void Listen()
        {
            try
            {
                bool offline = true;
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(() => offline = Offline));
                while (!offline)
                {
                    TcpObject tcpObject = new TcpObject();
                    IFormatter formatter = new BinaryFormatter();
                    tcpObject = (TcpObject)formatter.Deserialize(serverStream);

                    if (tcpObject.Command == Command.Transfer)
                    {
                        SentAntenna sentAntenna = (SentAntenna)tcpObject.Object;

                        int idx = 0;
                        foreach (string name in SharedProperties.AntennaNames)
                        {
                            if (name == sentAntenna.Name)
                                break;
                            idx++;
                        }

                        if (idx < 9)
                        {
                            PointCollection pointCollection = new PointCollection();
                            foreach (Frequency f in sentAntenna.Frequencies)
                                pointCollection.Add(new Point(f.Channel, f.Intensity));

                            SharedProperties.AntennaPoints[idx] = pointCollection;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // raise an event
            }
        }

serialization code:

case Command.Transfer:
                            Console.WriteLine("Transfering");
                            Thread transfer = new Thread(new ThreadStart(delegate
                            {
                                try
                                {
                                    string aName = tcpObject.Object.ToString();
                                    int indx = 0;
                                    foreach (string name in names)
                                    {
                                        if (name == aName)
                                            break;
                                        indx++;
                                    }

                                    if (indx < 9)
                                    {
                                        while (true) // need to kill when the father thread terminates
                                        {
                                            if (antennas[indx].Frequencies != null)
                                            {
                                                lock (antennas[indx].Frequencies)
                                                {
                                                    TcpObject sendTcpObject = new TcpObject();
                                                    sendTcpObject.Command = Command.Transfer;
                                                    SentAntenna sa = new SentAntenna(antennas[indx].Frequencies, aName);
                                                    sendTcpObject.Object = sa;
                                                    formatter.Serialize(networkStream, sendTcpObject);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex) { Console.WriteLine(ex); }
                            }));
                            transfer.Start();
                            break;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

Interesting. There's nothing particularly odd in your serialization code, and I've seen people use vanilla concatenation for multiple objects in the past, although I've actually always advised against it as BinaryFormatter does not explicitly claim this scenario is OK. But: if it isn't, the only thing I can suggest is to implement your own framing; so your write code becomes:

  • serialize to an empty MemoryStream
  • note the length and write the length to the NetworkStream, for example as a simple fixed-width 32-bit network-byte-order integer
  • write the payload from the MemoryStream to the NetworkStream
  • rinse, repeat

And the read code becomes:

  • read exactly 4 bytes and compute the length
  • buffer that many bytes into a MemoryStream
  • deserialize from the NetworkStream

(Noting in both cases to set the MemoryStream's position back to 0 between write and read)

You can also implement a Stream-subclass that caps the length if you want to avoid a buffer when reading, bit that is more complex.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...