c# NetworkStream 的 write() 和 read()

标签 c# tcpclient networkstream

我想知道如何阻止 Networkstream.Read() 阻塞线程。我有单独的线程,其中 NetworkStream.Read() 正在等待来自服务器的数据。假设用户按下某个按钮将一些数据发送到服务器。但是当其他线程中有 NetworkStream.Read() 等待数据时,我无法调用 NetworkStream.Write()。我每次都可以锁定 NetworkStream,但是 NetworkStream.Read() 会阻塞线程,因此我无法发送数据,直到读取至少 1 个字节。

最佳答案

您可以同时发出 readwrite,如 MSDN docs 的文档中所述

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

关于c# NetworkStream 的 write() 和 read(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843064/

相关文章:

c# - 编码字符未正确呈现

c# - Windows Phone 7 - 加密的 SQLite

c# - BeginRead 内存不足异常

C# TcpClient 更新(对于 IRC 客户端)

c# - 将 tcp 连接作为参数传递给不同的计算机

c# - 使用 C# 和 ASP.NET 从 Gmail/Hotmail/Yahoo 导入地址簿

c# - 从文件中读取值

c# - 如何在与 SignalGo 套接字断开连接后自动调用登录功能

c# - 从套接字读取网络流

c# - 即使服务器端没有数据,NetworkStream.DataAvailable 也为真