这就是我修复它的方法.....终于弄清楚了如何正确序列化和反序列化数据。
Dim c as New Cereal
c.text = "Testing...."
Dim bf As New BinaryFormatter
Dim ms As New MemoryStream
Dim b() As Byte
'serializes the Cereal to the created memory stream
bf.Serialize(ms, c)
'converts and moves into the byte array
b = ms.ToArray()
ms.Close()
'send the byte array
SyncLock mobjClient.GetStream
mobjClient.GetStream.Write(b, 0, b.Length)
End SyncLock
这是反序列化...
Dim intCount As Integer
Try
SyncLock mobjClient.GetStream
'returns the number of bytes to know how many to read
intCount = mobjClient.GetStream.EndRead(ar)
End SyncLock
'if no bytes then we are disconnected
If intCount < 1 Then
MarkAsDisconnected()
Exit Sub
End If
Dim bf As New BinaryFormatter
Dim c As New Cereal
'when data first comes on the stream it is sent to arData, then this puts into memory stream
Dim ms As New MemoryStream(arData)
'cut off the memory stream at the end of the data
ms.SetLength(intCount)
'deserialize to the created Cereal, giving us a replica of what was sent
c = bf.Deserialize(ms)
ms.Close()
'starts the listener again
mobjClient.GetStream.BeginRead(arData, 0, 3145728, AddressOf DoRead, Nothing)
If c.text IsNot Nothing Then
DisplayText(c.text)
ElseIf c.OppUserID IsNot Nothing Then
OnConnected(c.OppUserID, c.OppGender)
ElseIf c.command IsNot Nothing Then
OnCommandReceived(c)
ElseIf c.hasDeck = True Or c.hasBsDeck = True Or c.hasHand = True Then
OnDeckReceived(c)
ElseIf c.discard = True Then
OnDiscard(c)
Else
OnCardReceived(c)
End If
Catch e As Exception
MarkAsDisconnected()
End Try