什么时候的TcpClient的的NetworkStream完成一次读操作? [英] When does TcpClient's NetworkStream finish one read operation?

查看:613
本文介绍了什么时候的TcpClient的的NetworkStream完成一次读操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作是通过TCP和谷歌协议缓冲区涉及的客户端服务器通信的项目。在客户端,我基本上都采用NetworkStream.Read()做经由一个字节数组缓冲服务器阻塞读取。

I am working on a project that involves client server communication via TCP and Google Protocol Buffer. On the client side, I am basically using NetworkStream.Read() to do blocking read from server via a byte array buffer.

根据MSDN文档,

此方法读取数据到缓冲区参数,并返回成功读取的字节数。如果没有数据可供读取,读取方法返回0。如可用,到由大小参数指定的字节数的读操作读取尽可能多的数据。如果远程主机关闭连接,以及所有可用数据已经收到,Read方法立即完成并返回零字节。

This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the Read method returns 0. The Read operation reads as much data as is available, up to the number of bytes specified by the size parameter. If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes.

这是相同的情况下,与异步读取(NetworkStream.BeginRead和EndRead)。我的问题是,什么时候阅读()/ EndRead()返回?这似乎是在缓冲区中的所有字节都被填满后,将返回。但在我自己的测试,即并非如此。在一个操作中读取的字节数有很大的差异。我觉得很有道理,因为如果发送消息时有在服务器端暂停,客户不应该等到读取缓冲区已被填满。不读()/ EndRead()本身有一些超时机制?

It is the same case with async read (NetworkStream.BeginRead and EndRead). My question is that when does Read()/EndRead() return? It seems like it will return after all the bytes in the buffer have been filled. But in my own testing, that is not the case. The bytes read in one operation vary a lot. I think it makes sense because if there is a pause on the server side when sending messages, the client should not wait until the read buffer has been filled. Does the Read()/EndRead() inherently have some timeout mechanism?

我试图找出如何单中的NetworkStream实现read()和跟踪,直到EXTERN方法Receive_internal()被调用。

I was trying to find out how Mono implements Read() in NetworkStream and traced until a extern method Receive_internal() is called.

推荐答案

它读取所有可用上的NetworkStream或当缓冲区已满的数据。以先到者为准。你已经注意到了这个问题。

It reads all the data that is available on the networkstream or when the buffer is full. Whichever comes first. You have already noticed this behaviour.

所以,你将需要处理的所有字节,看看消息是否齐全。您可以通过取景消息做到这一点。请参阅有关异步套接字操作和消息帧 .NET的问题>你如何能做到这一点。

So you will need to process all the bytes and see whether the message is complete. You do this by framing a message. See .NET question about asynchronous socket operations and message framing on how you can do this.

至于超时问题,如果假设你问一个的BeginRead是否有超时,我会说不,因为它只是等待数据流上的到达并把它放入缓冲液,之后就可以处理输入的字节。

As for the timeout question, if assuming you are asking whether a beginread has a timeout, I would say no, because it is just waiting for data to arrive on the stream and put it into a buffer, after which you can process the incoming bytes.

字节可在读取操作的数目取决于类似的事情在网络(如延迟,代理节流)和客户端发送数据。

The number of bytes available on the read action depends on things like your network (e.g. latency, proxy throttling) and the client sending the data.

的BeginRead行为总结:


  1. 呼叫的BeginRead (); - >等待字节流上到达......

  2. 1字节以上已经赶到流

  3. 开始把步骤2成被赋予

  4. 缓冲区的字节
  5. 调用EndRead(); - >缓冲区内的字节(S)可以通过EndRead()进行处理;

  6. 最常见的做法是再次重复所有这些步骤

  1. Call BeginRead(); -> Waiting for bytes to arrive on the stream......
  2. 1 byte or more have arrived on the stream
  3. Start putting the byte(s) from step 2 into the buffer that was given
  4. Call EndRead(); -> The byte(s) within the buffer can be processed by EndRead();
  5. Most common practice is to repeat all these steps again.

这篇关于什么时候的TcpClient的的NetworkStream完成一次读操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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