日期:2014-05-17 浏览次数:20831 次
Socket socketCommunication = null;
private void Watch()
{
while (true)
{
socketCommunication = socketWatch.Accept();
dictCommunication.Add(socketCommunication.RemoteEndPoint.ToString(), socketCommunication);
lbSocketOnline.Items.Add(socketCommunication.RemoteEndPoint.ToString());
// 此处这个 thread 每次都会在新客户端连接后 new 出新对象,但我并没有保存这个 thread 的引用,理论上应该已经丢失引用成为垃圾了
Thread thread = new Thread(() =>
{
while (true)
{
byte[] bytes = new byte[1024 * 1024 * 2];
int length = socketCommunication.Receive(bytes);
string msg = Encoding.UTF8.GetString(bytes, 0, length);
ShowMsg("接收到来自" + socketCommunication.RemoteEndPoint.ToString() + "的数据:" + msg);
}
});
thread.IsBackground = true;
thread.Start();
ShowMsg("客户端连接成功!通信地址为:" + socketCommunication.RemoteEndPoint.ToString());
}
}