Ok, So I'm not an expert or anywhere close. I write in VB as a hobby and have written plenty of small apps and the majority of them are for old serial device connections.
Now those devices are connecting via TCP. Easy, So I thought...
My server is a embedded SBC and very structured in its communication protocol. All data terminates with a fixed character ETX (ChrW(3)). So I should be able to get incoming response stream and save it to a string until the ETX arrives.
This also is a single click button, connect-send-receive sub.
Having a separate connect/disconnect button would be helpful as sometime multiple commands would be sent and connecting/disconnecting repeatedly seems ridiculous.
Any help would be great.
Kevin
Shared Sub Connect(server As [String], port as [Int32], message As [String])
Try
Dim client As New TcpClient(server, port)
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
Dim stream As NetworkStream = client.GetStream()
stream.Write(data, 0, data.Length)
RTB1_update("Sent: {0}", message)
data = New [Byte](256) {}
Dim responseData As [String] = [String].Empty
Threading.thread.sleep(2000)
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
RTB1_update("Received: {0}", responseData)
stream.Close()
client.Close()
Catch e As ArgumentNullException
RTB1_update("ArgumentNullException: {0}", e)
Catch e As SocketException
RTB1_update("SocketException: {0}", e)
End Try
End Sub
What I have tried:
No matter what I do it fails to do this and waiting for a timeout approx 90 seconds is a joke. The code below is straight off of MSDN and completes long before the data even arrives. Adding a thread.sleep for a second or two helps, but some data is long and gets clipped, some is very short and the long unneeded wait is silly,