从 NetworkStream 读取特定数量的字节

发布于 2024-12-06 11:29:40 字数 1803 浏览 1 评论 0原文

我正在尝试从网络流中读取已知长度的消息。 我有点期待 NetworkStream.Read() 会等待返回,直到我提供给它的缓冲区数组已满。如果不是,那么 ReadTimeout 属性有什么意义呢?

我用来测试我的理论的示例代码

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));
        }
    }
}

结果是:

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

是否有一种标准方法可以进行仅在读取指定数量的字节时才返回的同步读取?我确信自己编写一个并不会太复杂,但是 TcpClientNetworkStream 上都存在超时属性,这表明它应该已经以这种方式工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-12-13 11:29:40

您所保证的是(其中之一):

  • 0 字节(流结束)
  • 至少 1 字节(一些数据可用;并不意味着没有更多数据可用或已经可用)
  • 错误(超时等)

读取指定的数据字节数...循环:

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();

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();
赢得她心 2024-12-13 11:29:40

TCP 是一种字节流协议,不保留应用程序消息边界。它根本无法以这种方式将字节“粘合”在一起。读取超时的目的是指定您希望读取阻塞多长时间。但只要能返回至少一个字节的数据,读操作就不会阻塞。

如果您需要循环调用 read 直到读完一条完整的消息,请执行此操作。 TCP 层并不关心你认为什么是完整的消息,这不是它的工作。

TCP is a byte-stream protocol that does not preserve application message boundaries. It is simply not able to "glue" bytes together in that way. The purpose of the read timeout is to specify how long you would like the read to block. But as long as at least one byte of data can be returned, the read operation will not block.

If you need to call read in a loop until you read a complete message, do that. The TCP layer doesn't care what you consider to be a full message, that's not its job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文