Click here to Skip to main content
15,891,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


VB
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,
Posted
Updated 28-Jan-18 1:21am
v2

1 solution

You are making an invalid assumption in your code. See NetworkStream.Read Method (Byte[], Int32, Int32) (System.Net.Sockets)[^], where it explains how much (if any) data is returned on the read command. Do not assume that you will receive all the data on a single read call. TCP/IP is not like reading a file, as you can only read whatever has been sent across the network, which may be nothing.
 
Share this answer
 
Comments
CPallini 28-Jan-18 7:23am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900