Search code examples
.netvb.nettcpclienttcpsocket

tcp client in vb.net not receiving the entire data response data from server


I have a small program which is a tcp client. I send a string from this client to a device over ethernet (it acts as the tcp server). As soon as the device recieves the input string it will respond back with response data. My problem is i am not getting the entire response data back from the server. (device).

   Dim serverStream As NetworkStream = clientSocket2.GetStream()
   Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("my-cmd")
   serverStream.Write(outStream, 0, outStream.Length)

   'serverStream.Flush()
   Dim inStream(clientSocket2.ReceiveBufferSize) As Byte
   serverStream.Read(inStream, 0, CInt(clientSocket2.ReceiveBufferSize))        
   returndata = System.Text.Encoding.ASCII.GetString(instream)

Returndata does not have the full response back from the server(device)


Solution

  • Actually it was very simple. I just gave some delay before reading the stream. The problem was that before the entire stream could be read the program execution came to the next line. A little delay made sure that the entire stream of data is retrieved. Thanks anyway