- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我继承了一些循环通过 BinaryReader 的响应的代码,并且它在一段时间内工作正常(返回 2 个字节),但是客户端需要一段时间才能响应(我假设)并且代码失败进入捕获逻辑。
我找不到任何关于 ReadByte() 将等待多长时间的文档,它似乎等待大约 3 秒,然后失败。
有人知道 ReadByte 的具体工作原理吗?我可以将它配置为以某种方式等待更长的时间吗?我的代码在下面,谢谢。
public virtual Byte[] Send(Byte[] buffer, Int32 recSize) {
Byte[] rbuffer = new Byte[recSize];
var binaryWriter = new BinaryWriter(stream);
var binaryReader = new BinaryReader(stream);
Int32 index = 0;
try {
binaryWriter.Write(buffer);
do {
rbuffer[index] = binaryReader.ReadByte(); // Read 1 byte from the stream
index++;
} while (index < recSize);
} catch (Exception ex) {
Log.Error(ex);
return rbuffer;
}
return rbuffer;
}
PS - 代码中的 recSize 为 2,它总是期望返回 2 个字节。
最佳答案
BinaryReader
本身没有超时,它只是底层流的包装器。超时的事情是您作为 stream
传入的任何流。您必须修改该对象的超时时间(如果该流也只是另一个包装器,则修改它的父对象)。
你根本不需要使用 BinaryReader 来做你想做的事情,同时假设 buffer
是一个 byte[]
你不需要 BinaryWriter要么。
Byte[] rbuffer = new Byte[recSize];
try {
stream.Write(buffer, 0, buffer.Length);
Int32 index = 0;
do
{
index += stream.Read(rbuffer, index, rbuffer.Length - index);
} while (index < recSize);
} catch (Exception ex) {
Log.Error(ex);
return rbuffer; //I would either let the exception bubble up or return null here, that way you can tell the diffrence between a exception and an array full of 0's being read.
}
关于.net - BinaryReader ReadByte() 是否超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409808/
我在搜索 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 时,偶尔会出现流损坏,其中正在读取的流不同步。
我是一名优秀的程序员,十分优秀!