假如客户端发送消息:长度(int 四个字节) + 包体(10个字节)
服务器接收:
byte[] bytes = new byte[1000];
接收:
this.socket.BeginReceive(headerByte, 0, bytes .Length, SocketFlags.None, CallAsyncCallback, this.socket);
回调:
private void CallAsyncCallback(IAsyncResult ar)
{
this.socket = (Socket)ar.AsyncState;
int count = this.socket.EndReceive(ar); //当收到数据后回调,这个count一定就是四个字节,如果网络非常慢,它只收到一个字节怎么办?
if(count!=0)
{
int header = BitConverter.ToInt32(bytes);
其实 我很想问你们一下,你们确定,这里能马上收到四个字节?如果count只收到了1个字节,那能转成int吗?能取长度吗?
}
}
网上大神能帮我解决一下这个问题吗?,头部占四个字节,也不能保证一次性就能收到四个字节呀,网上没有一个地方能说明白的,