NetworkStream如何在两个方向上工作? [英] How does NetworkStream work in two directions?

查看:122
本文介绍了NetworkStream如何在两个方向上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一个Tcp Echo Server的示例,对我来说有些不清楚.

I've read an example of a Tcp Echo Server and some things are unclear to me.

TcpClient client = null;
NetworkStream netStream = null;

try {
  client = listener.AcceptTcpClient(); 
  netStream = client.GetStream();

  int totalBytesEchoed = 0;
  while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0) {
    netStream.Write(rcvBuffer, 0, bytesRcvd);
    totalBytesEchoed += bytesRcvd;
  }

  netStream.Close();
  client.Close();
} catch {
  netStream.Close();
}

服务器收到数据包(while循环)时,他将数据读入rcvBuffer并将其写入流中.

When the server receives a packet (the while loop), he reads the data into rcvBuffer and writes it to the stream.

让我困惑的是消息在通讯中的时间顺序.是将用netStream.Write()写入的数据立即发送给客户端(甚至可能仍在发送),还是仅在已处理(由客户端)已写入流中的数据发送给客户端之后.

What confuses me is the chronological order of messages in communication. Is the data which was written with netStream.Write() sent immediately to the client (who may even still be sending), or only after the data which is already written to the stream (by client) processed.

以下问题甚至可以澄清前面的问题:如果客户端通过写入流发送了一些数据,那么该数据是否已移到服务器端的消息队列中等待读取,因此流实际上是空"的?那可以解释为什么服务器可以立即写入流-因为来自流的数据实际上是在其他地方缓冲的??

The following question may even clarify the previous: If a client sends some data by writing to the stream, is that data moved to the message queue on the server side waiting to be read so the stream is actually "empty"? That would explain why the server can immediately write to stream - because the data which comes from the stream is actually buffered elsewhere...?

推荐答案

提示:在该示例中,方法调用NetworkStream.Read被阻止.

Hint: The method call NetworkStream.Read is blocking in that example.

这本书是绝对正确的-对TCP流的原始访问并不意味着任何额外的分块",例如,在此示例中,一次可以很容易地处理一个字节.但是,分批执行读取和写入操作(通常使用暴露的缓冲区)可以提高处理效率(通常是由于较少的系统调用).网络层和网络硬件也采用了自己的缓冲区形式.

The book is absolutely correct -- raw access to TCP streams does not imply any sort of extra "chunking" and, in this example for instance, a single byte could easily be processed at a time. However, performing the reading and writing in batches (normally with exposed buffers) can allow for more efficient processing (often as a result of less system calls). The network layer and network hardware also employ there own forms of buffers.

实际上并不能保证从Write()写入的数据实际上会在更多Reads()成功完成之前被写入:即使将数据刷新到一层中也并不意味着它会刷新到另一层中,并且绝对不能保证数据已经返回给客户端.这是高层协议起作用的地方.

There is actually no guarantee that data written from Write() will actually be written before more Reads() successfully complete: even if data is flushed in one layer it does not imply it is flushed in another and there is absolutely no guarantee that the data has made its way back over to the client. This is where higher-level protocols come into play.

在此回显示例中,只是简单地尽可能快地推送数据.写入和读取都将基于基础网络堆栈(尤其是发送和接收缓冲区)而阻塞,每个堆栈都有自己的一系列缓冲区.

With this echo example the data is simply shoved through as fast as it can be. Both the Write and the Read will block based upon the underlying network stack (the send and receive buffers in particular), each with their own series of buffers.

[这当然简化了一些事情,人们总是可以看看TCP [protocol]本身,它确实将传输特性强加于实际的数据包流上.]

[This simplifies things a bit of course -- one could always look at the TCP [protocol] itself which does impose transmission characteristics on the actual packet flow.]

这篇关于NetworkStream如何在两个方向上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆