I have various client device with Ip address as below.
Ip address: 192.168.1.1; 192.168.1.2; 192.168.1.3; 192.168.1.4; 192.168.1.5;
With below peice of code i could able to receive data on richtextbox one device at time. My intention is to open client port all at same time and receive the data on respective textbox
For example
ip:192.168.1.1 data record = richtextbox1
ip:192.168.1.2 data record = richtextbox2
ip:192.168.1.3 data record = richtextbox3
ip:192.168.1.4 data record = richtextbox4
Code:Imports System.Net, System.Net.Sockets Public Class Form1 Dim clientSocket As Socket Dim byteData(1023) As Byte Private Sub OnConnect(ByVal ar As IAsyncResult) clientSocket.EndConnect(ar) clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _ New AsyncCallback(AddressOf OnRecieve), clientSocket) End Sub Private Sub OnRecieve(ByVal ar As IAsyncResult) Dim client As Socket = ar.AsyncState client.EndReceive(ar) Dim bytesRec As Byte() = byteData Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec) Read(message) clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _ New AsyncCallback(AddressOf OnRecieve), clientSocket) End Sub Delegate Sub _Read(ByVal msg As String) Private Sub Read(ByVal msg As String) If InvokeRequired Then Invoke(New _Read(AddressOf Read), msg) Exit Sub End If RichTextBox1.Text &= msg End Sub Private Sub btnConnect_Click_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect_Click.Click clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Dim ipAddress As IPAddress = ipAddress.Parse("192.168.1.1") Dim ipEndPoint As IPEndPoint = New IPEndPoint(ipAddress, 8899) clientSocket.BeginConnect(ipEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing) End Sub End Class




Reply With Quote
