c# Socket怎样才可以把数据接收完整啊?

2024-11-08 22:44:35
推荐回答(1个)
回答1:

、、、这样,Receive的返回值是接收的字节数,当是0时表示传送结束,否则循环接收
缓冲区不用太大,8k或64k看情况
Byte[] RecvBytes = new Byte[1024*8];
int iBytes = 1;
string strAllHtml = "";
while (iBytes > 0)
{
iBytes = conn.Receive(RecvBytes, RecvBytes.Length, 0);
strAllHtml += Encoding.UTF8.GetString(RecvBytes, 0, iBytes);
}