错误,

  while (true)
                {
                    byte[] startMsg = new byte[5];
                    while (client.Receive(startMsg) > 0)//有问题,不一定返回5
                    {
                        string msg = Encoding.ASCII.GetString(startMsg);
                        if (msg.EndsWith("<^F^>"))
                            break;
                    }
                    byte[] bt = new byte[4];
                    int avNum = 0;
                    int count = 0;
                    int sizeOnce = 1024;
                    client.Receive(bt);//不一定读取4个字节,查看MSDN
                    int lenght = BitConverter.ToInt32(bt, 0);
                    byte[] bit = new byte[lenght];
                    while ((avNum = client.Receive(bit, count, sizeOnce, SocketFlags.None)) > 0)
                    {
                        count = count + avNum;
                        if (count + sizeOnce < lenght)
                        {
                            sizeOnce = 1024;
                        }
                        else
                        {
                            sizeOnce = lenght - count;
                            break;
                        }

                    }



//MSDN

如果当前使用的是面向连接的 Socket,那么 Receive 方法将会读取所有可用的数据,直到达到缓冲区的大小为止。

意思是如果协议栈中可用数据大于buff那么读完buff大小返回,如果协议栈暂时有的数据小于buff读完协议栈返回。

如果面向无连接的,UDP, 协议栈大小》buff, 读完buff其余的丢弃

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值