0

我有一个名为 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

主机监听活动流,当有东西放在上面时,它应该将其反序列化为此处显示的新 Cereal

'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 流。我是序列化/反序列化和使用 Visual Studio 11 的新手

4

1 回答 1

0

我的 arData 只接受 1024 字节,正如您在代码中看到的那样,当它再次启动侦听器时。从那以后我已经多次提出这个问题,因为现在我通过流发送图像。我现在超过 3,000,000(或 3MB),我认为我发送的最多大约是 2MB。在我的代码中,有 5 或 6 个地方需要更新 arData,而不仅仅是上面显示的那个。此外,提高这一点似乎并没有减慢任何极小数据的发送速度。

于 2013-01-09T19:29:05.093 回答