- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以通过相同的底层 NetworkStream 同时使用 BinaryReader 和 BinaryWriter
(我的目标是通过 TcpClient 连接同时发送和接收数据)
到目前为止,我遇到了两个相关的帖子:
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.
Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes might be read more than once, bytes might be skipped, or character reading might become unpredictable.
我不是 100% 确定如何解释这些引述,我也不确定我上面的 2 个案例中的哪一个是可能的。
最佳答案
"is"是简短的回答; NetworkStream
本质上充当双工流,读取操作与写入操作完全分开。 BinaryReader
将只使用读取操作,BinaryWriter
将使用写入操作。 Length
、Position
和 Seek
等概念在 NetworkStream
上没有意义,因此不受支持。所以:这里使用BinaryReader
/BinaryWriter
应该没有冲突。然而,与此同时,我可能会建议反对使用它们 - 仅仅是因为与使用原始 Stream
操作相比,它们实际上通常不会增加太多,并且与任意网络协议(protocol)。如果您正在实现仅使用您的代码的自定义内容:当然,您可能没问题。
有关在使用 BinaryReader
/BinaryWriter
时触摸 Stream
的警告仍然非常适用于某些其他流- 例如 FileStream
:当读取器/写入器认为它负责时,您可以通过重新定位流来简单地破坏数据 - 因为只有一个位置:这意味着读取会影响写入等. 但是:这根本不适用于NetworkStream
。在这方面:NetworkStream
是异常(exception),而不是规则:就您通常对 Stream
的期望而言,NetworkStream
是非常不寻常的。
关于c# - NetworkStream 上的 BinaryReader 和 BinaryWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49251939/
我在搜索 BinaryReader.Skip 函数时遇到了这个 feature request on msdn .他说您可以通过使用它来提供自己的 BinaryReader.Skip() 函数。 只看
我有一个基于套接字的应用程序,它使用 BinaryReader 公开接收到的数据客户端的对象。我一直在尝试调试读取器中包含的数据不干净的问题......即我正在读取的缓冲区包含超过新数据大小的旧数据。
Controller : private readonly Dictionary streams; public ActionResult Upload(string qqfile,
我想尝试基本的文件加密和压缩,所以我的第一个问题是 ByteReader 是否是适合该作业的正确阅读器?我的第二个最重要的问题是:使用二进制阅读器时如何循环直到文件末尾?有没有办法像 How read
我需要将 int 数组作为二进制写入输出文件,并在 C++ Linux 的程序中将二进制数据作为 int 读取,类似于 C# 中的 BinaryReader 和 BinaryWriter。我怎样才能做
我正在做家庭作业,但完全卡住了!我想要做的是使用已经定义的输入并使用 saveDataTo() 方法将其保存到文件中,然后使用 readDataFrom() 方法读取输入。 我卡在了第一部分。我不确定
我目前正在编写一个缓存 BaseStream.Position 和 BaseStream.Length 属性的 BinaryReader。这是我目前所拥有的: public class FastBin
我正在使用以下代码序列化数据表中的数据。 var rows = new List>(); 我正在填充 DataTable 中的行并将它们放入字典中。不要问为什么:) using(var fileStr
我正在开发一个解析器来接收 UDP 信息、解析它并存储它。为此,我使用了 BinaryReader,因为它主要是二进制信息。其中一些将是字符串。 MSDN says对于 ReadString() 函数
案例: 再次尝试通过我的 NIC 捕获数据包, 我开发了 2 个扩展用于捕获可变位数 public static string ReadBits ( this BinaryReader Key
当使用 System.IO.BinaryReader 读取二进制文件时,使用任何多字节读取方法时字节顺序似乎不受影响。 BinaryReader br = new BinaryReader(fs, S
我写了下面的方法来使用二进制模式将文件归档到一个文件中: // Compile archive public void CompileArchive(string Fi
我有一个二进制字节数组。在 C# 中,使用 BinaryReader 和 ReadInt32 读取它非常容易(这正是我所需要的)。像这样: reader = new BinaryReader ( ne
我正在继承 BinaryReader 类。 我必须覆盖一些基本方法,例如 ReadUInt16 . 该方法的内部实现是: public virtual ushort ReadUInt16(){
在 C、C++ 中,如果您以二进制方式写入一个整数(例如 fwrite(&someInt,sizeof(int),1,fp))说到文件,您有很多问题需要考虑你想从不同的机器上读取那个整数——例如,字节
BinaryReader 没有 EndOfStream 属性。使用以下代码检查是否已到达流结尾是否安全? reader.BaseStream.Length>reader.BaseStream.Posi
我继承了一些循环通过 BinaryReader 的响应的代码,并且它在一段时间内工作正常(返回 2 个字节),但是客户端需要一段时间才能响应(我假设)并且代码失败进入捕获逻辑。 我找不到任何关于 Re
我想弄清楚我在这里做错了什么。我正在尝试使用 Binary Reader 来轻松地将流中的初始四个字节转换为 Int32 值,该值告诉我其余数据的预期长度。 static void Main(stri
我正在尝试解压缩字节数组并使用二进制读取器将其转换为字符串。执行以下代码时,inStream位置从0变为数组长度,但str始终为空字符串。 BinaryReader br = null; string
我认为 BinaryReader.ReadChars() 方法存在问题。当我在原始套接字 NetworkStream 周围包装 BinaryReader 时,偶尔会出现流损坏,其中正在读取的流不同步。
我是一名优秀的程序员,十分优秀!