ここにいくつかのパブリックフィールドが表示されたCerealというシリアル化可能なクラスがあります
<Serializable> Public Class Cereal
Public id As Integer
Public cardType As Type
Public attacker As String
Public defender As String
Public placedOn As String
Public attack As Boolean
Public placed As Boolean
Public played As Boolean
Public text As String
Public Sub New()
End Sub
End Class
私のクライアントコンピューターは、ここに示されているようにシリアル化することにより、新しいシリアルをホストに送信しています。
'sends data to host stream (c1)
Private Sub cSendText(ByVal Data As String)
Dim bf As New BinaryFormatter
Dim c As New Cereal
c.text = Data
bf.Serialize(mobjClient.GetStream, c)
End Sub
ホストはアクティビティのストリームをリッスンし、何かがストリームに追加されると、ここに示す新しいシリアルに逆シリアル化することになっています。
'accepts data sent from the client, raised when data on host stream (c2)
Private Sub DoReceive(ByVal ar As IAsyncResult)
Dim intCount As Integer
Try
'find how many byte is data
SyncLock mobjClient.GetStream
intCount = mobjClient.GetStream.EndRead(ar)
End SyncLock
'if none, we are disconnected
If intCount < 1 Then
RaiseEvent Disconnected(Me)
Exit Sub
End If
Dim bf As New BinaryFormatter
Dim c As New Cereal
c = CType(bf.Deserialize(mobjClient.GetStream), Cereal)
If c.text.Length > 0 Then
RaiseEvent LineReceived(Me, c.text)
Else
RaiseEvent CardReceived(Me, c)
End If
'starts listening for action on stream again
SyncLock mobjClient.GetStream
mobjClient.GetStream.BeginRead(arData, 0, 1024, AddressOf DoReceive, Nothing)
End SyncLock
Catch e As Exception
RaiseEvent Disconnected(Me)
End Try
End Sub
次の行を実行すると、System.OutOfMemoryExceptionが発生し、これが機能しない理由がわかりません。
c = CType(bf.Deserialize(mobjClient.GetStream), Cereal)
ストリームはTCPClientストリームです。シリアル化/逆シリアル化とVisualStudio11の使用は初めてです