deserialize system.outofmemoryexception on a networkstream

时光毁灭记忆、已成空白 提交于 2020-01-07 05:09:14

问题


I've got a serializeable class called Cereal with several public fields shown here

<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

My client computer is sending a new Cereal to the host by serializing it shown here

'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

The host listens to the stream for activity and when something gets put on it, it is supposed to deserialize it to a new Cereal shown here

'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

when the following line executes, I get a System.OutOfMemoryException and I cannot figure out why this isn't working.

c = CType(bf.Deserialize(mobjClient.GetStream), Cereal)

The stream is a TCPClient stream. I'm new to serialization/deserialization and using visual studio 11


回答1:


my arData was only accepting 1024 bytes as you can see in the code when it starts the listener again. I have since raised this many times because now I send Images across the streams. I'm at over 3,000,000 now (or 3MB), and I think the most I've sent was about 2MB. In my code there were 5 or 6 places where arData needed to be updated, not just the one shown above. Also, raising this did not seem to slow down any sending of extremely small data.



来源:https://stackoverflow.com/questions/13547316/deserialize-system-outofmemoryexception-on-a-networkstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!