将NetworkStream复制到MemoryStream需要适应性∞天 [英] Copying NetworkStream to MemoryStream takes infitity ∞ days

查看:128
本文介绍了将NetworkStream复制到MemoryStream需要适应性∞天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下代码:

_clientRequestStream = _tcpClient.GetStream();

var memoryStream = new MemoryStream();
_clientRequestStream.CopyTo(memoryStream);

CopyTo需要很长时间才能将Stream复制到另一个Stream中.看来应用程序没有任何原因就停在那里,或者至少我找不到原因.

CopyTo takes long long long time to copy a Stream into another Stream. It seems application stops there without any reason or at least I couldn't find the reason.

推荐答案

网络流一直保持打开状态,直到被流的一端关闭为止. CopyTo()复制流中的所有数据,直到流结束.如果服务器未发送数据,则流不会结束或关闭,并且CopyTo()会忠实地等待更多数据或流结束.流另一端的服务器必须关闭流才能使其结束,并返回CopyTo().

A network stream remains open until it is closed by one end of the stream. CopyTo() copies all data from the stream, waiting until the stream ends. If the server is not sending data, the stream does not end or close and CopyTo() dutifully waits for more data or for the stream to end. The server on the other end of the stream must close the stream for it to end and CopyTo() to return.

Google的"TcpClient教程"或"TcpCLient示例"获得了一些不错的页面,这些页面显示了您可以使用它们的其他方式,例如检查NetworkStream.DataAvailable以查看是否有数据等待或流是否仍在打开且没有数据.要仅读取一些数据而不等待流关闭,可以使用NetworkStream.Read()或将其包装在StreamReader中并使用ReadLine().这完全取决于您要连接的服务器以及您要完成的工作.

Google "TcpClient Tutorial" or "TcpCLient Sample" to get some good pages showing other ways you might use them, such as checking NetworkStream.DataAvailable to see if there is data waiting or if the stream is still open with no data. To just read some data and not wait for the stream to close you would use NetworkStream.Read() or wrap it in a StreamReader and use ReadLine(). It all depends on the server you are connecting to and what you are trying to accomplish.

这篇关于将NetworkStream复制到MemoryStream需要适应性∞天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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