Threaded Async programming TCP Client Server
by Mountain Computers Inc., Publication Date: Monday, December 31, 2018
View Count: 1481, Keywords: TCP, Client, Server, Async, Programming, Threaded, Hashtags: #TCP #Client #Server #Async #Programming #Threaded
talk about a new calculus of programming, learning to write in Async threaded TCP is interesting.
just the
Invoke command alone, is interesting. feels like learning calculus to an ole programmer like me, but I get it. parallel processing, multi-threaded, no longer linear tasking and Application.DoEvents() just does not do the trick anymore.. ha!! for some of my older programmers who still just do basic stuff and the thought of multi-tasking multi-threaded parallel tasking, aside from my Unix programmers who were doing tasking back in 1960s and 70s.
if they still exist. check out the two examples found at the drop box of hc-kr.com ,
client,
server
there are a few more examples for tcp client server async invoke programming at Microsoft similar to these that are just as valuable.
=====================
server
=====================
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim Listning As TcpListener
Dim Allclient As TcpClient
Dim clientList As New List(Of ClassforClient)
Dim pReader As StreamReader
Dim pClient As ClassforClient
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Listning = New TcpListener(IPAddress.Any, 3818)
Listning.Start()
UpdateList("Server Starting", False)
Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
End Sub
' create a delegate
Delegate Sub _cUpdate(ByVal str As String, ByVal relay As Boolean)
Sub UpdateList(ByVal str As String, ByVal relay As Boolean)
On Error Resume Next
If InvokeRequired Then
Invoke(New _cUpdate(AddressOf UpdateList), str, relay)
Else
TextBox1.AppendText(str & vbNewLine)
' if relay we will send a string
If relay Then send(str)
End If
End Sub
Sub send(ByVal str As String)
For x As Integer = 0 To clientList.Count - 1
Try
clientList(x).Send(str)
Catch ex As Exception
clientList.RemoveAt(x)
End Try
Next
End Sub
Sub AcceptClient(ByVal ar As IAsyncResult)
pClient = New ClassforClient(Listning.EndAcceptTcpClient(ar))
AddHandler(pClient.getMessage), AddressOf MessageReceived
AddHandler(pClient.clientLogout), AddressOf ClientExited
clientList.Add(pClient)
UpdateList("New Client Joined!", True)
Listning.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listning)
End Sub
Sub MessageReceived(ByVal str As String)
UpdateList(str, True)
End Sub
Sub ClientExited(ByVal client As ClassforClient)
clientList.Remove(client)
UpdateList("Client Exited!", True)
End Sub
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
UpdateList("Server says : " & TextBox2.Text, True)
TextBox2.Clear()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UpdateList("Server says : " & TextBox2.Text, True)
TextBox2.Clear()
End Sub
End Class
=====================
client
=====================
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim client As TcpClient
Dim sWriter As StreamWriter
Dim NIckFrefix As Integer = New Random().Next(1111, 9999)
Sub xLoad() Handles Me.Load
Me.Text &= " " & NIckFrefix
End Sub
Delegate Sub _xUpdate(ByVal str As String)
Sub xUpdate(ByVal str As String)
If InvokeRequired Then
Invoke(New _xUpdate(AddressOf xUpdate), str)
Else
TextBox3.AppendText(str & vbNewLine)
End If
End Sub
Sub read(ByVal ar As IAsyncResult)
Try
xUpdate(New StreamReader(client.GetStream).ReadLine)
client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing)
Catch ex As Exception
xUpdate("You have disconnecting from server")
Exit Sub
End Try
End Sub
Private Sub send(ByVal str As String)
Try
sWriter = New StreamWriter(client.GetStream)
sWriter.WriteLine(str)
sWriter.Flush()
Catch ex As Exception
xUpdate("You're not server")
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Connect" Then
Try
client = New TcpClient(TextBox1.Text, CInt(TextBox2.Text))
client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf read), Nothing)
Button1.Text = "Disconnect"
Catch ex As Exception
xUpdate("Can't connect to the server!")
End Try
Else
client.Client.Close()
client = Nothing
Button1.Text = "Connect"
End If
End Sub
Private Sub TextBox4_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox4.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
send(NIckFrefix & " says : " & TextBox4.Text)
TextBox4.Clear()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
send(NIckFrefix & " says : " & TextBox4.Text)
TextBox4.Clear()
End Sub
End Class
=====================
end
more to come...
if you found this article helpful, consider contributing $10, 20 an Andrew Jackson or so..to the author. more authors coming soon
FYI we use paypal or patreon, patreon has 3x the transaction fees, so we don't, not yet.
© 2024 myBlog™ v1.1 All rights reserved. We count views as reads, so let's not over think it.