Visual studio 在网络流上反序列化system.outofmemoryexception
我有一个名为谷物的可序列化类,这里显示了几个公共字段Visual studio 在网络流上反序列化system.outofmemoryexception,visual-studio,serialization,deserialization,out-of-memory,binaryformatter,Visual Studio,Serialization,Deserialization,Out Of Memory,Binaryformatter,我有一个名为谷物的可序列化类,这里显示了几个公共字段 <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
<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
主机监听流中的活动,当有东西放在流中时,它应该将其反序列化为此处显示的新消息
<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
该流是一个TCPClient流。我不熟悉序列化/反序列化,使用visual studio 11时,我的arData只接受1024字节,当它再次启动侦听器时,您可以在代码中看到这一点。我已经多次提出这个问题,因为现在我通过流发送图像。我现在已经超过300万(或3MB),我想我发送的最多大约是2MB。在我的代码中,有5到6个地方需要更新arData,而不仅仅是上面显示的那个地方。此外,提高这一点似乎并没有减缓任何发送极小数据的速度