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;
}
{
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其余的丢弃