-
-
[求助]C# NetworkStream循环为什么是收到数据循环,而不是一直循环?
-
发表于:
2017-3-19 17:19
3559
-
[求助]C# NetworkStream循环为什么是收到数据循环,而不是一直循环?
本人新手初学C#网络编程,发现了一个很奇怪的问题,就是在while循环中下断,居然不断下来,只有当收到数据时才会断下来
private void AccepMessage()
{
NetworkStream netStream = new NetworkStream(socket);
while (true)
{
try
{
byte[] datasize = new byte[4];
netStream.Read(datasize, 0, 4);//我在此处下断,不会断下来,只有当收到数据时才会断下来
int size = System.BitConverter.ToInt32(datasize, 0);
Byte[] message = new byte[size];
int dataleft = size;
int start = 0;
while (dataleft > 0)
{
int recv = netStream.Read(message, start, dataleft);
start += recv;
dataleft -= recv;
}
this.rchTxtBoxReceive.Text = System.Text.Encoding.Unicode.GetString(message);
}
catch
{
break;
}
}
}
private void btnRequest_Click(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(AccepMessage));
thread.Start();
}
请问这是为什么?
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!