gpt4 book ai didi

.net - BinaryReader ReadByte() 是否超时?

转载 作者:可可西里 更新时间:2023-11-01 02:49:15 25 4
gpt4 key购买 nike

我继承了一些循环通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com