Multiple Serializarion over Network (tcp)

Arsium ***** 331 Reputation points
2020-12-15T17:56:12.817+00:00

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 ?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 114.2K Reputation points
    2020-12-15T18:56:42.59+00:00

    Maybe try something like this:

    Public Sub D()
       Dim client = T.AcceptTcpClient()
       Task.Run(Sub() ThreadProc2(client))
    
       client = T.AcceptTcpClient()
       Task.Run(Sub() ThreadProc(client))
    End Sub
    

    Add the loop and adjust the code if you have a collection of files to be sent.