Question Convert a simple client/server app from sync to async

Bokka

New member
Joined
Jun 21, 2019
Messages
1
Programming Experience
Beginner
Hello!


I ask you if someone could help me out. I wrote a couple of programs (a client and a server) that communicate with each other. Something really basic. The problem, however, is that being a synchronous communication, the server starts only after it has received the package from the client and ends the communication. I would therefore like to make this program (server) asynchronous, to be able to make the best use of it. Here is the code:

SERVER:

VB.NET:
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Public Class Form1

    Dim aa As SocketAsyncOperation
    Dim ip As IPAddress = IPAddress.Parse(“127.0.0.1”)
    Dim tl As TcpListener = New TcpListener(ip, 1234)
    Dim ns As NetworkStream
    Dim br As BinaryReader
    Dim bw As BinaryWriter
    Dim soc As Socket

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        Label1.Text = "ok"
        tl.Start()
        Label1.Text = "Server started"
        soc = tl.AcceptSocket
        ns = New NetworkStream(soc)
        bw = New BinaryWriter(ns)
        br = New BinaryReader(ns)
        Dim s As String = br.ReadString
        bw.Write(s)

    End Sub
End Class

CLIENT:

VB.NET:
Imports System.Net.Sockets
Imports System.IO

Public Class Form1

    Dim tc As TcpClient = New TcpClient()
    Dim ns As NetworkStream
    Dim br As BinaryReader
    Dim bw As BinaryWriter

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load



    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        tc.Connect(“127.0.0.1”, 1234)
        ns = tc.GetStream
        br = New BinaryReader(ns)
        bw = New BinaryWriter(ns)
        bw.Write(TextBox1.Text)
        TextBox2.Text = br.ReadString

    End Sub
End Class



I saw that in vb.net there is the function called "SocketAsync" but I didn't find any example to follow ...

Please, someone could help me?

Thanks in advance!!
 
Top Bottom