Asynchronous Operator | TCP | Serialization

  • Thread starter Thread starter Arsium
  • Start date Start date
A

Arsium

Guest
Hello dear Sirs,

I need your help to make my server and client asynch.


Here is the code

Dll to serialize :

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


The server send 2 files and client need to receive them but here nothing is happening. No file is written (MSG & FILE). The only way to avoid problem I have found is to set a synclock here :

Private Sub ThreadProc(ByVal obj As Object)

SyncLock T

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 SyncLock
End Sub

Private Sub ThreadProc2(ByVal obj As Object)

SyncLock T

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 SyncLock

End Sub

Continue reading...
 
Back
Top