...基本上正是标题所说的。NetworkStream.Length 未实现。有什么替代方案?
我正在尝试进行一系列递归的异步回调,这些回调包含对 NetworkStream.BeginRead(...) 的调用。要知道我何时达到基本情况并收到所有字节,我需要知道流中字节的长度。有解决方法吗?
代码(此代码的入口点是在 TcpListern.BeginAcceptTcpClient 调用之后编写的。:
private void StartRead(IAsyncResult ar)
{
try
{
//Do an initial read:
AsyncClient client = (AsyncClient)ar.AsyncState;
int amountRead = 0;
try
{
amountRead = client.ClientStream.EndRead(ar);
}
catch (IOException io)
{
ProcessDebugLog("Async read complete", io);
client.ClientStream.Close();
return;
}
string text = Encoding.UTF8.GetString(client.Bytes, 0, amountRead);
//If TCP segmentation has occurred, more blocks will have
// to get read in:
if (client.ClientStream.Position < client.ClientStream.Length /*EXCEPTION HERE*/)
{
try
{
client.ClientStream.BeginRead(client.Bytes, 0, client.Bytes.Length, StartRead, client);
}
catch (IOException io)
{
ProcessDebugLog("Async read complete", io);
client.ClientStream.Close();
}
}
else
{
client.ClientStream.Close();
}
}
catch (Exception ex)
{
ProcessDebugLog("ERROR - StartRead", ex);
}
}