Search code examples
vb.netserializationtcpclienttcplistener

VB.net serialization across a network


I am attempting to Serialize a class across a network but I keep getting the following error.

System.Runtime.Serialization.SerializationException: 'End of Stream encountered before parsing was completed.'

I have seen several threads that suggest things like turning the network stream into a memory stream to allow you to set the position to 0 but this did not work for me.

My code is

'Receiver

Private Sub ReceivingMethod()

    Dim f As BinaryFormatter = New BinaryFormatter()

    Dim msg As clsMessage

    msg = f.Deserialize(TCPClient.GetStream())

    ProcessMessage(msg)

    Threading.Thread.Sleep(50)

End Sub

.

'Sender

Private Sub SendingMethod()

    do while MessageQueue.count > 0

        'Get the first message from the queue
        Dim msg As clsMessage = MessageQueue(0)

        Dim f As BinaryFormatter = New BinaryFormatter()

            f.Serialize(TCPClient.GetStream(), msg)

            MessageQueue.Remove(msg)

        End If

        Threading.Thread.Sleep(50)

    Loop
End Sub

Any help would be greatly appreciated.


Solution

  • I have found that protobuf-net is easily implemented and much easier than playing with binary formatter. I initially thought 'how hard could it be' but it then become apparent why external serialization has been created.

    Thanks all.