Solved

Binary Stream '0' does not contain a valid BinaryHeader Exception

Posted on 2007-03-19
5
1,392 Views
Last Modified: 2012-08-13
I wrote a code in vb.net that uses a serialized class, asynchronous read . I am getting a Binary Stream '0' does not contain a valid BinaryHeader Exception error
once it hits the line   msgReceived = CType(formatter.Deserialize(receivedMemoryStrm), MessagePacket)
 in the streamreceiver function. Can someone help ?tks

<Serializable()> Public Class MessagePacket

    Public msgType As CmdMsgType
    Public msgNotification As CmdMsgNotifications
    Public msgSenderName As String
    Public msgData As String
    Public msgFileName As String
    Public msgFileNameId As String
    Public msgFileData As Byte()
    Public msgFileNameLength As Integer

 Public Sub New(ByVal mType As CmdMsgType, ByVal myNotification As CmdMsgNotifications, ByVal user As String, ByVal myFileName As String, ByVal id As String, ByVal data As Byte(), ByVal myLength As Integer)
        Me.msgType = mType
        Me.msgNotification = myNotification
        Me.msgSenderName = user
        Me.msgFileName = myFileName
        Me.msgFileNameId = id
        Me.msgFileData = data
        Me.msgFileNameLength = myLength
    End Sub
 Public Sub New()
        Me.msgType = Nothing
        Me.msgNotification = Nothing
        Me.msgSenderName = ""
        Me.msgFileNameId = ""
        Me.msgFileNameId = ""
        Me.msgFileData = Nothing
        Me.msgData = ""
        Me.msgFileNameLength = 0
    End Sub
end class

public myclass
  Const READ_BUFFER_SIZE As Integer = 60000000 'the sending or receiving buffer can only contain 60mb of data
  Private readBuffer(READ_BUFFER_SIZE) As Byte
 
Public Sub sendFile(ByVal connection As Connection, ByVal filename As String, ByVal fileId As Guid)
       
            Dim sourcePath As String
            Dim fs As FileStream
            Dim command As String
            Dim commandLength As Integer
            Dim dataToSend As Byte()
            Dim fileData As Byte()
                 Dim fileCommandLength As Integer
            Dim msg As MessagePacket


            'collect the path of the file to sent
          sourcePath = "C:\" & filename
            fs = File.Open(sourcePath, FileMode.Open)
            fileCommandLength = CType(fs.Length, Integer)

            If fileCommandLength < READ_BUFFER_SIZE Then
                ReDim fileData(CType(fs.Length, Integer))
                fs.Read(fileData, 0, fileData.Length)
                msg = New MessagePacket(PacketType.CmdMsgType.Messenger, PacketType.CmdMsgNotifications.TransmitFile, Sender, filename, fileId.ToString, fileData, fs.Length)
                fs.Close()
                SendData(msg)
end sub


  Private Sub StreamReceiver(ByVal ar As IAsyncResult)
        Dim bytesRead As Integer
        Dim msgReceived As MessagePacket
        Dim receivedMemoryStrm As MemoryStream
        Dim formatter As New BinaryFormatter


        Try
                     SyncLock client.GetStream
                    ' Finish asynchronous read into readBuffer and get number of bytes read.
                    bytesRead = client.GetStream.EndRead(ar)
                End SyncLock
                If bytesRead > 0 Then

                    receivedMemoryStrm = New MemoryStream(readBuffer)
                    receivedMemoryStrm.Position = 0
                    msgReceived = CType(formatter.Deserialize(receivedMemoryStrm), MessagePacket)
                    receivedMemoryStrm.Close()
 End If

             
                    SyncLock client.GetStream
                        ' Start a new asynchronous read into readBuffer.
                        client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
                    End SyncLock
            end sub
end class
0
Comment
Question by:websavvy
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:jef06
Comment Utility
You might have change this class, and now he can't serialize back the old object.

Try this and tell me if you get an error it is in c# convert it to vb. I use this to clone serializable object.
/// <summary>
        /// Permet de copier un objet
        /// </summary>
        /// <param name="objet">Objet à copier</param>
        /// <returns>L'objet</returns>
        public static object Clone(object objet)
        {
            MemoryStream mem = new MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            // Sérialize l'objet      
            binFormat.Serialize(mem, objet);

            mem.Seek(0, System.IO.SeekOrigin.Begin);
            // Désérialize l'objet
            return binFormat.Deserialize(mem);
        }
0
 

Author Comment

by:websavvy
Comment Utility
It still does not work. What is so weird i do not get that error when i transfer file whose size is  less than 15kb.
0
 
LVL 5

Expert Comment

by:jef06
Comment Utility
does CmdMsgType and CmdMsgNotifications are serializable?
0
 

Author Comment

by:websavvy
Comment Utility
actually i did not serialize the CmdMsgType and CmdMsgNotifications . i put the classes below
Public Class PacketType
    Public Enum CmdMsgType
        listen
    End Enum
    Public Enum CmdMsgNotifications
        Start
       End
          End Enum
 
    End Class



<Serializable()> Public Class MessagePacket

    Public msgType As CmdMsgType
    Public msgNotification As CmdMsgNotifications
    Public msgSenderName As String
    Public msgData As String
    Public msgFileName As String
    Public msgFileNameId As String
    Public msgFileData As Byte()
    Public msgFileNameLength As Integer

    Public Sub New()
        Me.msgType = Nothing
        Me.msgNotification = Nothing
        Me.msgSenderName = ""
        Me.msgFileNameId = ""
        Me.msgFileNameId = ""
        Me.msgFileData = Nothing
        Me.msgData = ""
        Me.msgFileNameLength = 0
    End Sub
    Public Sub New(ByVal mType As CmdMsgType, ByVal myNotification As CmdMsgNotifications)
        Me.msgType = mType
        Me.msgNotification = myNotification
    End Sub
    Public Sub New(ByVal mType As CmdMsgType, ByVal myNotification As CmdMsgNotifications, ByVal user As String)
        Me.msgType = mType
        Me.msgNotification = myNotification
        Me.msgSenderName = user
    End Sub

    Public Sub New(ByVal mType As CmdMsgType, ByVal myNotification As CmdMsgNotifications, ByVal user As String, ByVal data As String)

        Me.msgType = mType
        Me.msgNotification = myNotification
        Me.msgSenderName = user
        Me.msgData = data


    End Sub

    Public Sub New(ByVal mType As CmdMsgType, ByVal myNotification As CmdMsgNotifications, ByVal user As String, ByVal myFileName As String, ByVal id As String)
        Me.msgType = mType
        Me.msgNotification = myNotification
        Me.msgSenderName = user
        Me.msgFileName = myFileName
        Me.msgFileNameId = id

    End Sub
    Public Sub New(ByVal mType As CmdMsgType, ByVal myNotification As CmdMsgNotifications, ByVal user As String, ByVal myFileName As String, ByVal id As String, ByVal data As Byte(), ByVal myLength As Integer)
        Me.msgType = mType
        Me.msgNotification = myNotification
        Me.msgSenderName = user
        Me.msgFileName = myFileName
        Me.msgFileNameId = id
        dim i as integer
        Me.msgFileData = data
        Me.msgFileNameLength = myLength
    End Sub
    Public Property MessageType() As CmdMsgType
        Get
            Return msgType
        End Get
        Set(ByVal Value As CmdMsgType)
            msgType = Value
        End Set
    End Property

    Public Property Notification() As CmdMsgNotifications
        Get
            Return msgNotification
        End Get
        Set(ByVal Value As CmdMsgNotifications)
            msgNotification = Value
        End Set
    End Property

    Public Property SenderName() As String
        Get
            Return msgSenderName
        End Get
        Set(ByVal Value As String)
            msgSenderName = Value
        End Set
    End Property
    Public Property FileName() As String
        Get
            Return msgFileName
        End Get
        Set(ByVal Value As String)
            msgFileName = Value
        End Set
    End Property

    Public Property FileNameId() As String
        Get
            Return msgFileNameId
        End Get
        Set(ByVal Value As String)
            msgFileNameId = Value
        End Set
    End Property
    Public Property FileNameLength() As Integer
        Get
            Return msgFileNameLength
        End Get
        Set(ByVal Value As Integer)
            msgFileNameLength = Value
        End Set
    End Property
    Public Property MsgContent() As String

        Get
            Return msgData
        End Get
        Set(ByVal Value As String)
            msgData = Value
        End Set
    End Property
    Public Property MsgFileContent() As Byte()

        Get
            Return msgFileData
        End Get
        Set(ByVal Value As Byte())
            msgFileData = Value
        End Set
    End Property

End Class
0
 
LVL 5

Accepted Solution

by:
jef06 earned 500 total points
Comment Utility
Would you mind to add the attribute serializable and see if that solve the problem
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now