Hello Dear Sir , I got some problem with parallel serialization (in same time , not async) :
Dll Serialized :
Public Class Class1
<Serializable()>
Public Class PacketMaker
Public Property File As Byte()
Public Property Type_Packet As PacketType
End Class
Public Enum PacketType As Integer
MSG = &H5057
File = &H5056
End Enum
End Class
Server :
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Text
Imports System.Threading
Imports ClassLibrary1.Class1
Public Class Form1
Public T As TcpListener = New TcpListener(IPAddress.Any, 8080)
Private client As TcpClient
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
T.Start()
Task.Run(Sub() D())
End Sub
Public Sub D()
While True
client = T.AcceptTcpClient()
Task.Run(Sub() ThreadProc2(client))
Task.Run(Sub() ThreadProc(client))
End While
End Sub
Private Sub ThreadProc(ByVal obj As Object)
Dim client = CType(obj, TcpClient)
Dim B As BinaryFormatter = New BinaryFormatter()
Dim p As PacketMaker = New PacketMaker()
p.Type_Packet = PacketType.MSG
p.File = IO.File.ReadAllBytes("File1.zip")
B.Serialize(client.GetStream(), p)
End Sub
Private Sub ThreadProc2(ByVal obj As Object)
Dim client = CType(obj, TcpClient)
Dim B As BinaryFormatter = New BinaryFormatter()
Dim p As PacketMaker = New PacketMaker()
p.Type_Packet = PacketType.File
p.File = IO.File.ReadAllBytes("File2.zip")
B.Serialize(client.GetStream(), p)
End Sub
End Class
Client :
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.Serialization.Formatters.Binary
Imports ClassLibrary1.Class1
Public Class Form1
Private T As TcpClient = New TcpClient()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim address As String = "192.168.0.15"
T.Connect(IPAddress.Parse(address), 8080)
Dim N As NetworkStream = T.GetStream()
Task.Run(Sub() R(N))
End Sub
Public Sub R(ByVal L As NetworkStream)
Dim sf As BinaryFormatter = New BinaryFormatter()
While True
Dim po As PacketMaker = CType(sf.Deserialize(L), PacketMaker)
IO.File.WriteAllBytes(po.Type_Packet.ToString(), po.File)
End While
End Sub
End Class
When I tried this , I got problems: when I launched the 2 tasks to send files , client side seems not to get the files because of 2 tasks in parallel. However I tried to use synclock server sided tasks but I really want to send the 2 files with binaryformatter in same time , any ideas ?