......基本上标题所说的是什么。 NetworkStream.Length未实现。什么是替代方案?.NET 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);
}
}
你是否控制连接的两端? – 2012-04-03 20:07:46
有些流真的是流。你不知道那条河里有多少水,直到水耗尽。 – 2012-04-03 20:17:23
@Boo是的我控制双方。 – kmarks2 2012-04-03 20:20:07