The code below reads everything that needs to be read from the tcp client stream, and at the next iteration it will just sit there in Read () (I assume I'm waiting for data). How can I guarantee that this is not the case and just comes back when there is nothing to read? Should I set a low timeout and respond to an exception when it fails? Or is there a better way?
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(ip, port);
Stream stm = tcpclnt.GetStream();
stm.Write(cmdBuffer, 0, cmdBuffer.Length);
byte[] response = new Byte[2048];
MemoryStream ms = new MemoryStream();
byte[] buffer = new Byte[2048];
int length;
while ((length = stm.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, length);
byte[] response = ms.ToArray();
tcpclnt.Close();
source
share