Search code examples
c#tcptcpclient

tcp ip client read received data c#


TcpClient client = new(SERVER_IP, PORT_NO);
        NetworkStream stream = client.GetStream();
        stream.Write(WriteArry, 0, WriteArry.Length);
        
        byte[] data = new byte[10];
        
        int recivedbyte = stream.Read(data, 0, 10);

how can i check if Length of received data less than 10 wait more for Remaining data? or Data to received more than once? Thanks


Solution

  • byte[] data = new byte[10];
    
    int read, remaining = data.Length;
    while (remaining > 0 && (read =
        stream.Read(data, data.Length - remaining, remaining)) > 0)
    {
        remaining -= read;
    }
    if (remaining != 0) throw new EndOfStreamException(); // EOF