我繼承了一些循環來自BinaryReader的響應的代碼,並且它工作正常(返回2個字節)一段時間,但客戶端需要一段時間來響應(我假設)並且代碼落入catch邏輯。BinaryReader ReadByte()有沒有超時?
我找不到任何有關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個字節。
您可以包含此方法的函數定義以及發生失敗時引發的異常的詳細信息。 –
當然@ScottChamberlain,我剛剛更新了它 –