Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. BinaryFormatter -> Invalid binaryHeader

BinaryFormatter -> Invalid binaryHeader

Scheduled Pinned Locked Moved C#
help
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hansipet
    wrote on last edited by
    #1

    Hello, I have a strange problem. I want to send some objectes over Ethernet (UDP One packet is one object) After that I Deserialize the data, but I get allways an exception that there is an Invalid binaryheader. I don't have any idea how I can solve it. my code is int size = socket.Receive(buf); BinaryFormatter serializer = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buf,0,size); ms.Seek(0, SeekOrigin.Begin); LogMessage logMessage = (LogMessage)serializer.Deserialize(ms); Console::WriteLine("{0}", logMessage.ToString()); ms.Close(); and the sending function: MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, logMessage); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] data = ms.ToArray(); socket.Send(data); ms.Close(); I have checked the MemoryStream and there is the whole data available (Sending and receiving) LogMessage Best regards Hansjörg

    S L 2 Replies Last reply
    0
    • H hansipet

      Hello, I have a strange problem. I want to send some objectes over Ethernet (UDP One packet is one object) After that I Deserialize the data, but I get allways an exception that there is an Invalid binaryheader. I don't have any idea how I can solve it. my code is int size = socket.Receive(buf); BinaryFormatter serializer = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buf,0,size); ms.Seek(0, SeekOrigin.Begin); LogMessage logMessage = (LogMessage)serializer.Deserialize(ms); Console::WriteLine("{0}", logMessage.ToString()); ms.Close(); and the sending function: MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, logMessage); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] data = ms.ToArray(); socket.Send(data); ms.Close(); I have checked the MemoryStream and there is the whole data available (Sending and receiving) LogMessage Best regards Hansjörg

      S Offline
      S Offline
      Soleil couchant
      wrote on last edited by
      #2

      Are you serializing and deserializing using the same .NET Frameowrk / Operating System? If you are serializing in, for instance, .NET Framework and the deserializing in .NET Compact Framework, this will be an issue. Otherwise, you can use the XML Serializer, if platform/framework is your problem

      H 1 Reply Last reply
      0
      • S Soleil couchant

        Are you serializing and deserializing using the same .NET Frameowrk / Operating System? If you are serializing in, for instance, .NET Framework and the deserializing in .NET Compact Framework, this will be an issue. Otherwise, you can use the XML Serializer, if platform/framework is your problem

        H Offline
        H Offline
        hansipet
        wrote on last edited by
        #3

        I use the same computer, same plattform and the both projects are in the same solution from vs2005.. I want to use the binary formatter, because I think it is faster and this is needed in my case. Best regards Hansjörg

        R 1 Reply Last reply
        0
        • H hansipet

          I use the same computer, same plattform and the both projects are in the same solution from vs2005.. I want to use the binary formatter, because I think it is faster and this is needed in my case. Best regards Hansjörg

          R Offline
          R Offline
          Rudolf Jan
          wrote on last edited by
          #4

          Maybe it is a good idea to use the SOAP serializer first for debugging purposes. It is easier to review to XML then. It is not very difficult to switch back to the binary serializer once it all works.

          H 1 Reply Last reply
          0
          • H hansipet

            Hello, I have a strange problem. I want to send some objectes over Ethernet (UDP One packet is one object) After that I Deserialize the data, but I get allways an exception that there is an Invalid binaryheader. I don't have any idea how I can solve it. my code is int size = socket.Receive(buf); BinaryFormatter serializer = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buf,0,size); ms.Seek(0, SeekOrigin.Begin); LogMessage logMessage = (LogMessage)serializer.Deserialize(ms); Console::WriteLine("{0}", logMessage.ToString()); ms.Close(); and the sending function: MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, logMessage); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] data = ms.ToArray(); socket.Send(data); ms.Close(); I have checked the MemoryStream and there is the whole data available (Sending and receiving) LogMessage Best regards Hansjörg

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            I have seen some funny stuff when closing MemoryStream, remove those .Close() for now. If that is the problem, what does work is placing it in a using block.

            **

            xacc.ide-0.2.0.74 - now with C# 3.5 support!

            **

            H 1 Reply Last reply
            0
            • L leppie

              I have seen some funny stuff when closing MemoryStream, remove those .Close() for now. If that is the problem, what does work is placing it in a using block.

              **

              xacc.ide-0.2.0.74 - now with C# 3.5 support!

              **

              H Offline
              H Offline
              hansipet
              wrote on last edited by
              #6

              It doesn't help... With the XmlSerializer it works...(but there I have a problem with the DateTime..I have to set there the XmlIgnoreattribute Best regards Hansjörg

              1 Reply Last reply
              0
              • R Rudolf Jan

                Maybe it is a good idea to use the SOAP serializer first for debugging purposes. It is easier to review to XML then. It is not very difficult to switch back to the binary serializer once it all works.

                H Offline
                H Offline
                hansipet
                wrote on last edited by
                #7

                with the soap formatter I have one problem. If I serialize my object then I get a NullReferenceException. The problem it seems that it is a DateTime field. If this field is not inside the class (other data are strings) all works fine. Do you have any idea? Best regards Hansjörg

                R 1 Reply Last reply
                0
                • H hansipet

                  with the soap formatter I have one problem. If I serialize my object then I get a NullReferenceException. The problem it seems that it is a DateTime field. If this field is not inside the class (other data are strings) all works fine. Do you have any idea? Best regards Hansjörg

                  R Offline
                  R Offline
                  Rudolf Jan
                  wrote on last edited by
                  #8

                  I'm not very experienced with this topic. A NullReferenceExpetion means that soemthing is wrong with an object. Likely causes are that you forgot to assign memory (using new) or maybe you try to use an object that no longer exists. Best way to solve this is stepping through the suspected code using the debugger and checking the object values.

                  H 1 Reply Last reply
                  0
                  • R Rudolf Jan

                    I'm not very experienced with this topic. A NullReferenceExpetion means that soemthing is wrong with an object. Likely causes are that you forgot to assign memory (using new) or maybe you try to use an object that no longer exists. Best way to solve this is stepping through the suspected code using the debugger and checking the object values.

                    H Offline
                    H Offline
                    hansipet
                    wrote on last edited by
                    #9

                    I have checked it...the object that gives the exception is a DateTime value...and this value is okay. I have no idea why... Best regards Hansjörg

                    R 1 Reply Last reply
                    0
                    • H hansipet

                      I have checked it...the object that gives the exception is a DateTime value...and this value is okay. I have no idea why... Best regards Hansjörg

                      R Offline
                      R Offline
                      Rudolf Jan
                      wrote on last edited by
                      #10

                      I saw you posted this question in another post. There you mentioned other parameters. Did you check these as well? Also check all other variables you use. Somtimes these errors really are obscure, but it is almost certain you did something wrong. You also may try slight code variations, just to see if these work. Fi replace parameters with constant values, replace the datetime by anonther type of object etc. Just keep trying. It may take you two or three days before you find out.

                      H 1 Reply Last reply
                      0
                      • R Rudolf Jan

                        I saw you posted this question in another post. There you mentioned other parameters. Did you check these as well? Also check all other variables you use. Somtimes these errors really are obscure, but it is almost certain you did something wrong. You also may try slight code variations, just to see if these work. Fi replace parameters with constant values, replace the datetime by anonther type of object etc. Just keep trying. It may take you two or three days before you find out.

                        H Offline
                        H Offline
                        hansipet
                        wrote on last edited by
                        #11

                        I have tried a lot of things and now I have found out the problem. For coding I use c++/cli and there it seems that it is not possible to use the DateTime^, but you have to use DateTime... Best regards Hansjörg

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups