C# socket 服务端半闭,客户端还在读数据导致内存一直涨
private void RevMsg()
{
while (isRevData)
{
while(netStream1!=null && netStream1.DataAvailable == true) //主要是加了这个条件判断,前后顺序还不能乱。
{
byte[] buffer =null;
try
{
buffer = new byte[2048];
int size = netStream1.Read(buffer, 0, buffer.Length);
strRev = System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length);
this.listBoxData.Invoke(new MethodInvoker(delegate
{
listBoxData.Items.Add(strRev);
}));
}
catch (System.Exception ex)
{
MessageBox.Show(isRevData.ToString());
isRevData = false;
}
}
}
Thread.Sleep(50);
}