-
Aug 1st, 2016, 04:38 AM
#1
Thread Starter
Junior Member
switching Ip address of client IP
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
-
Aug 1st, 2016, 05:59 AM
#2
Re: switching Ip address of client IP
I'm not sure, but it looks like this is the client side of a TCP connection and I haven't used the sockets directly (normally use tcpClient), but I would create a class, and copy most of the code you have there to it.
I would add a variable to the class to hold a reference to a textbox and the IP address to connect to.
In the btnConnect_Click Sub, I would instantiate five instances of the class, passing the IP address and textbox that the class is to handle.
Most of the class instantiation would be a modified version of what you have in the btnConnect_Click now, using the textbox and IpAddress passed to it in place of the hardcoded values.
You would modify the Sub Read to use the variable referencing the textbox, rather than the hardcoded textbox.
Don't have time to try to test, or give a more detailed example.
-
Aug 1st, 2016, 06:23 AM
#3
Thread Starter
Junior Member
Re: switching Ip address of client IP
with below peice o code i can update rich textbox, But it taking very long time. How can i reduce calculation speed and increase update speed.
Code:
Public Class Form_StringMain
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
counter = counter + 1
If counter = 1 Then
IPAddressss()
sender = Iparray(0)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 2 Then
IPAddressss()
sender = Iparray(1)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 3 Then
IPAddressss()
sender = Iparray(2)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 4 Then
IPAddressss()
sender = Iparray(3)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 5 Then
IPAddressss()
sender = Iparray(4)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 6 Then
IPAddressss()
sender = Iparray(5)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 7 Then
IPAddressss()
sender = Iparray(6)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 8 Then
IPAddressss()
sender = Iparray(7)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 9 Then
IPAddressss()
sender = Iparray(8)
'' MsgBox(sender)
Connect(sender, "8899")
End If
If counter = 10 Then
counter = 0
End If
End Sub
Private Sub IPAddressss()
IPArrays = "192.168.1.80 , 192.168.1.85 , 192.168.1.86 ,192.168.1.87 , 192.168.1.88 , 192.168.1.89 , 192.168.1.90 , 192.168.1.91 "
IPArray = Split(IPArrays, " , ")
End Sub
Sub Connect(ByVal server As [String], ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim Port1 As String
Port1 = 8899
Dim port As Int32 = CInt(Port1)
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
If server = "192.168.1.80" Then
RichTextBox1.Text = responseData
SMCB1_form1.Receieve_Msg_SMCB1(responseData)
End If
If server = "192.168.1.85" Then
RichTextBox2.Text = responseData
SMCB2_Form.Receieve_Msg_SMCB2(responseData)
End If
If server = "192.168.1.86" Then
''RichTextBox2.Text = responseData
SMCB3_Form.Receieve_Msg_SMCB3(responseData)
End If
If server = "192.168.1.87" Then
''RichTextBox2.Text = responseData
SMCB4_Form.Receieve_Msg_SMCB4(responseData)
End If
If server = "192.168.1.88" Then
''RichTextBox2.Text = responseData
SMCB5_Form.Receieve_Msg_SMCB5(responseData)
End If
If server = "192.168.1.89" Then
''RichTextBox2.Text = responseData
SMCB6_FORM.Receieve_Msg_SMCB6(responseData)
End If
If server = "192.168.1.90" Then
''RichTextBox2.Text = responseData
SMCB7_Form.Receieve_Msg_SMCB7(responseData)
End If
If server = "192.168.1.91" Then
''RichTextBox2.Text = responseData
SMCB8_Form.Receieve_Msg_SMCB8(responseData)
End If
' Close everything.
stream.Close()
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect
End Class
-
Aug 1st, 2016, 11:50 AM
#4
Re: switching Ip address of client IP
Do you need to close the connection each time?
Do you need to send a message to the server each time to initiate a request and then read the response?
You didn't send a message in your original code, and in this new code the message is just the port number (as a string) being sent to the server. Is this required?
I guess I'll assume that the 256 byte buffer is sufficient for your response and that the server is flushing its side so that you're receiving the whole response when generated, rather than getting a partial response.
Basically, if you want to speed this up, you should be kicking off several background tasks to do these operations in parallel, rather than sequentially on the GUI thread, using a timer.
Unfortunately, I can't actually test this, and there is probably a better way to do this, but I'll post example code that at least compiles and might work.
But, it won't work as is because I stubbed in Subs in the current form to take the place of the ones on form SMCB1_form1. I don't know if that is your main form, or another form.
The point was to try to create a class to do the common stuff, with parameters to pass in the things that are different per instance.
So, the attempt here is to pass in a RichTextbox to use, if desired, the method to be called, a reference to the form that contains the methods to be called (so they can be invoked on the GUI thread for that form, although it actually might be the same GUI thread for all forms, so perhaps any existing form reference would work).
Don't have time to verify whether the code would actually work, or crash and burn when run. Added a button so I could just try one iteration of the tasks (if I got the time). I would have to create a Server in order to test it, so its not likely to happen.
Code:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading.Tasks
Public Class Form1
Private counter As Integer
Private Iparray() As String = {"192.168.1.80", "192.168.1.85", "192.168.1.86", "192.168.1.87", "192.168.1.88", "192.168.1.89", "192.168.1.90", "192.168.1.91"}
Delegate Sub StringDelegate(s As String)
Private Sub Receieve_Msg_SMCB1(rsp As String)
End Sub
Private Sub Receieve_Msg_SMCB2(rsp As String)
End Sub
Private Sub Receieve_Msg_SMCB3(rsp As String)
End Sub
Private Sub Receieve_Msg_SMCB4(rsp As String)
End Sub
Private Sub Receieve_Msg_SMCB5(rsp As String)
End Sub
Private Sub Receieve_Msg_SMCB6(rsp As String)
End Sub
Private Sub Receieve_Msg_SMCB7(rsp As String)
End Sub
Private Sub Receieve_Msg_SMCB8(rsp As String)
End Sub
Private Class ServerComm
Private _IpAddr As IPAddress
Private _Port As Integer = 8899
Private _txtBox As RichTextBox
Private client As TcpClient
Private _callBack As StringDelegate
Private _frm As Form1
Public Sub New(IPAddr As String, CallBack As StringDelegate, frm As Form1, Optional txtBox As RichTextBox = Nothing)
_IpAddr = IPAddress.Parse(IPAddr)
client = New TcpClient(IPAddr, _Port)
_callBack = CallBack
_frm = frm
End Sub
Public Sub DoExchange()
Try
' Translate the passed message into ASCII and store it as a Byte array.
Dim Message As String = "8899"
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", Message)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
If _txtBox IsNot Nothing Then
_txtBox.BeginInvoke(Sub() _txtBox.Text = responseData)
End If
_frm.BeginInvoke(Sub() _callBack(responseData))
' Close everything.
stream.Close()
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
End Sub
End Class
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
KickOffTasks()
End Sub
Private Sub KickOffTasks()
Dim aRequest As ServerComm
aRequest = New ServerComm(Iparray(0), AddressOf Me.Receieve_Msg_SMCB1, Me, RichTextBox1)
Dim task1 As New Task(AddressOf aRequest.DoExchange)
task1.Start()
aRequest = New ServerComm(Iparray(1), AddressOf Me.Receieve_Msg_SMCB2, Me, RichTextBox2)
Dim task2 As New Task(AddressOf aRequest.DoExchange)
task2.Start()
aRequest = New ServerComm(Iparray(2), AddressOf Me.Receieve_Msg_SMCB3, Me)
Dim task3 As New Task(AddressOf aRequest.DoExchange)
task3.Start()
aRequest = New ServerComm(Iparray(3), AddressOf Me.Receieve_Msg_SMCB4, Me)
Dim task4 As New Task(AddressOf aRequest.DoExchange)
task4.Start()
aRequest = New ServerComm(Iparray(4), AddressOf Me.Receieve_Msg_SMCB5, Me)
Dim task5 As New Task(AddressOf aRequest.DoExchange)
task5.Start()
aRequest = New ServerComm(Iparray(5), AddressOf Me.Receieve_Msg_SMCB6, Me)
Dim task6 As New Task(AddressOf aRequest.DoExchange)
task6.Start()
aRequest = New ServerComm(Iparray(6), AddressOf Me.Receieve_Msg_SMCB7, Me)
Dim task7 As New Task(AddressOf aRequest.DoExchange)
task7.Start()
aRequest = New ServerComm(Iparray(7), AddressOf Me.Receieve_Msg_SMCB8, Me)
Dim task8 As New Task(AddressOf aRequest.DoExchange)
task8.Start()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
KickOffTasks()
End Sub
End Class
Last edited by passel; Aug 1st, 2016 at 12:03 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|