Solved

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

Posted on 2007-03-19
5
1,409 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
ID: 18747473
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
ID: 18751511
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
ID: 18754525
does CmdMsgType and CmdMsgNotifications are serializable?
0
 

Author Comment

by:websavvy
ID: 18756164
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
ID: 18756240
Would you mind to add the attribute serializable and see if that solve the problem
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

21 Experts available now in Live!

Get 1:1 Help Now