在网络流上反序列化system.outofmemoryexception [英] deserialize system.outofmemoryexception on a networkstream

查看:210
本文介绍了在网络流上反序列化system.outofmemoryexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Cereal的可序列化类,其中显示了几个公共字段

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

当执行以下行时,我得到了System.OutOfMemoryException,并且我无法弄清楚为什么这行不通.

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)

该流是TCPClient流.我是序列化/反序列化和使用Visual Studio 11的新手.

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

推荐答案

我的arData仅接受1024个字节,正如您在代码中再次启动侦听器时所看到的那样.此后,我已经提出了很多次,因为现在我将图像发送到各个流中.我现在有超过3,000,000(或3MB)的内存,我认为我发送的最大内存约为2MB.在我的代码中,有5到6个地方需要更新arData,而不仅仅是上面显示的地方.另外,提高这一点似乎也不会减慢任何发送非常小的数据的速度.

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.

这篇关于在网络流上反序列化system.outofmemoryexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆