C#C
C#3y ago
Shiv

❔ Read large data over TCP

Team need inputs on reading large data using TCPClient.
I need to read data using TCPClient , I am sending a command and receiving data. but not sure whether I have read complete data. How do i check whether all the data has been read and how do I get the buffer size at receiver end ..can I use int.Max like Byte[Int32.Max]. But i noticed I am not getting the whole data at once.Should I loop it until I get the complete data?

using (TcpClient client = new TcpClient("192.168.100.123", 10001))
{
using (NetworkStream stream = client.GetStream())
{
using (StreamReader reader = new StreamReader(stream))
{

byte[] outStream = System.Text.Encoding.ASCII.GetBytes(";");
stream.Write(outStream, 0, outStream.Length);

// Buffer to store the response bytes.
Byte[] data = new Byte[5000]; // how do i know this size

//string content = new StreamReader(stream, Encoding.Unicode).ReadToEnd();


Int32 bytes = stream.Read(data, 0, data.Length);
var responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
}
}
}
Was this page helpful?
❔ Read large data over TCP - C#