Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read specific number of bytes from NetworkStream

Tags:

stream

sockets

I am trying to read a message of known length from the network stream. I was kinda expecting that NetworkStream.Read() would wait to return until buffer array I gave to it is full. If not, then what is the point of the ReadTimeout property?

Sample code I am using to test my theory

public static void Main(string[] args)
{
    TcpListener listener = new TcpListener(IPAddress.Any, 10001);
    listener.Start();

    Console.WriteLine("Waiting for connection...");

    ThreadPool.QueueUserWorkItem(WriterThread);

    using (TcpClient client = listener.AcceptTcpClient())
    using (NetworkStream stream = client.GetStream())
    {
        Console.WriteLine("Connected. Waiting for data...");

        client.ReceiveTimeout = (int)new TimeSpan(0, 1, 0).TotalMilliseconds;
        stream.ReadTimeout = (int)new TimeSpan(0, 1, 0).TotalMilliseconds;

        byte[] buffer = new byte[1024];
        int bytesRead = stream.Read(buffer, 0, buffer.Length);

        Console.WriteLine("Got {0} bytes.", bytesRead);
    }

    listener.Stop();

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey(true);
}

private static void WriterThread(object state)
{
    using (TcpClient client = new TcpClient())
    {
        client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001));
        using (NetworkStream stream = client.GetStream())
        {
            byte[] bytes = Encoding.UTF8.GetBytes("obviously less than 1024 bytes");
            Console.WriteLine("Sending {0} bytes...", bytes.Length);
            stream.Write(bytes, 0, bytes.Length);
            Thread.Sleep(new TimeSpan(0, 2, 0));
        }
    }
}

Result of that is:

Waiting for connection...
Sending 30 bytes...
Connected. Waiting for data...
Got 30 bytes.
Press any key to exit...

Is there a standard way of making a sync read that returns only when specified number of bytes was read? I am sure it is not too complicated to write one myself, but presence of the timeout properties on both TcpClient and NetworkStream kinda suggests it should be already working that way.

like image 540
Ilia G Avatar asked Feb 23 '23 21:02

Ilia G


1 Answers

All you are guaranteed is (one of):

  • 0 bytes (end of stream)
  • at least 1 byte (some data available; does not mean there isn't more coming or already available)
  • an error (timeout, etc)

To read a specified number of bytes... loop:

int read = 0, offset = 0, toRead = ...
while(toRead > 0 && (read = stream.Read(buffer, offset, toRead)) > 0) {
    toRead -= read;
    offset += read;
}
if(toRead > 0) throw new EndOfStreamException();
like image 123
Marc Gravell Avatar answered Mar 06 '23 20:03

Marc Gravell