【发布时间】:2011-10-02 20:19:53
【问题描述】:
现在在我的代码中我有这样的东西:
state.BytesRead += readPacket.Result;
if (state.BytesRead < state.Data.Length)
{
Read(state);
}
else
{
nextRead(state);
}
但是,我注意到第一个 if 语句从未运行。我尝试做这样的事情,看看我是否可以让条件运行:
tcpClient.SendBufferSize = 16;
tcpClient.ReceiveBufferSize = 16;
但它仍然没有运行。我在客户端和服务器之间发送了大量数据(长度超过 1000 个字符的字符串),但条件仍然不会运行。看起来它总是一次读取所有字节。
这是因为我正在使用Task.Factory.FromAsync 并且它仅在读取所有字节时完成?这是调用我帖子顶部的代码 sn-p 的代码行:
Task<int>.Factory.FromAsync(state.Stream.BeginRead, state.Stream.EndRead, state.Data, state.BytesRead, state.Data.Length - state.BytesRead, state);
我在 FromAsync 之前使用了异步回调,所有教程都需要在继续下一次读取之前检查是否读取了所有字节。 FromAsync 不再需要这样做吗?它现在会自动处理吗?
【问题讨论】:
标签: c# android android-asynctask tcpclient