Hello all,
I am having a play with the sample plugin, and have set up some subs which handle connection to a server, as well as receiving data. I was hoping someone might be able to point me in the right direction, but what i would like to be able to do is expand the subs so that they support making multiple connections (through tcpclient), but also then so that i can expose data from the stream and know where it actually comes from.
So my question is:
Appreciate your thoughts, thanks guys
My code is below:
I am having a play with the sample plugin, and have set up some subs which handle connection to a server, as well as receiving data. I was hoping someone might be able to point me in the right direction, but what i would like to be able to do is expand the subs so that they support making multiple connections (through tcpclient), but also then so that i can expose data from the stream and know where it actually comes from.
So my question is:
- How can i go about creating a collection, arraylist (or whatever is appropriate) of TCPclients which each have their own server (e.g. can't use TCPlistener) which stay open and accept Async communications.
- If i can keep multiple clients open, how can i then identify the remote IP address and port of the device that the message came from? that is how can i for example write to the log "Message received from" IP:Port, Message so i can work out not just what the message is, but where it was from.
Appreciate your thoughts, thanks guys
My code is below:
Code:
Called in init hw Dim IP As String = "192.168.45.20" Connect(IP) Private Stream As NetworkStream Public Sub Connect(ByVal address As String) Dim Client As New TcpClient() Try ' Try to connect to the server on port 11000. Client.Connect(IPAddress.Parse(address), 9090) hs.WriteLog(IFACE_NAME & " TCP", "Client Connected to" & address) ' Retrieve the network stream. Stream = Client.GetStream() ' Create a new thread for receiving incoming messages. Dim ReceiveThread As New Thread(AddressOf ReceiveData) ReceiveThread.IsBackground = True ReceiveThread.Start() ' Create a BinaryWriter for writing to the stream. Dim w As New BinaryWriter(Stream) ' Loop until the word QUIT is entered. Dim Text As String Do Text = Console.ReadLine() ' Send the text to the remote client. If Text <> "QUIT" Then w.Write(Text) Loop Until Text = "QUIT" ' Close the connection socket. Client.Close() Catch Err As Exception Console.WriteLine(Err.ToString()) End Try End Sub Public Sub ReceiveData() Dim myReadBuffer(515) As Byte Dim myCompleteMessage As StringBuilder = New StringBuilder() Dim numberOfBytesRead As Integer = 0 ' Create a BinaryReader for the stream. Dim r As New BinaryReader(Stream) Do ' Display any received text. Try If Stream.DataAvailable Then numberOfBytesRead = Stream.Read(myReadBuffer, 0, myReadBuffer.Length) 'recieve the message and store it as myCompleteMessage myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)) hs.WriteLog(IFACE_NAME & " TCP Message", myCompleteMessage.ToString) Stream.Flush() myCompleteMessage.Length = 0 End If Catch Err As Exception Console.WriteLine(Err.ToString()) End Try Loop End Sub