c#tcpClient异步接收数据,数据量大的时候怎么办?怎么用缓存接收信息 20
clientStream=client.GetStream();//服务端套接字clientStream.BeginRead(buffer,0,MAX_LENGTH,ne...
clientStream = client.GetStream();//服务端套接字
clientStream.BeginRead(buffer, 0, MAX_LENGTH, new AsyncCallback(ReciveBack), null);
回调函数
private void ReciveBack(IAsyncResult ar)
{
private byte[] buffer = new byte[1024];//缓存数组
private MemoryStream memStream = new MemoryStream();//缓存
int bytesRead =clientStream.EndRead(ar);
memStream.Write(buffer, 0, bytesRead);//如果bytesRead 大于1024怎么办? clientStream.BeginRead(buffer, 0, MAX_LENGTH, new AsyncCallback(ReciveBack), null);
} 展开
clientStream.BeginRead(buffer, 0, MAX_LENGTH, new AsyncCallback(ReciveBack), null);
回调函数
private void ReciveBack(IAsyncResult ar)
{
private byte[] buffer = new byte[1024];//缓存数组
private MemoryStream memStream = new MemoryStream();//缓存
int bytesRead =clientStream.EndRead(ar);
memStream.Write(buffer, 0, bytesRead);//如果bytesRead 大于1024怎么办? clientStream.BeginRead(buffer, 0, MAX_LENGTH, new AsyncCallback(ReciveBack), null);
} 展开
1个回答
展开全部
直接把你的临时的缓存变成全局的不就行了,private byte[] buffer = new byte[1024];//缓存数组?
追问
本来我那个就是全局的,我提问了,我看见下面没有这个参数,我随手就从上面复制下来了,private 忘记弄掉了....
这样不好使....因为安卓那边是直接一次性发完的,我调试,发现 int bytesRead =clientStream.EndRead(ar); 只执行一次,你用个什么全局的收都没有用啊,麻烦些个代码行么
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询