0

我有一個可序列化類叫做穀物與衆多公共領域這裏反序列化的System.OutOfMemoryException

<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 
0這裏顯示了新的穀物

當下面的行執行時,我得到一個System.OutOfMemoryException,我不明白爲什麼這不起作用。

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

該流是TCPClient流。我是新的序列化/反序列化和使用Visual Studio 11

+0

De-Cerealise? :) –

回答

0

我arData只接受1024個字節,你可以在它再次啓動監聽器時看到代碼。自那之後,我已經提出了很多次,因爲現在我發送圖像在整個流中。我現在已經超過了3,000,000(或3MB),而且我認爲我發送的最多的大約是2MB。在我的代碼中,有5或6個需要更新arData的地方,而不僅僅是上面顯示的地方。另外,提高這個數據似乎並沒有減慢發送極小數據的速度。