private void thread() { while (islistening) { byte[] buffer = new byte[1024]; timer1.Start(); try { listener1.Bind(new IPEndPoint(IPAddress.Any, port)); listener1.Listen(2000); Console.WriteLine("等待客户端连接...."); while (true) { Socket sc = listener1.Accept();//接受一个连接 SocketStore ss = new SocketStore(); ss.key = sc.RemoteEndPoint.ToString(); ss.sc = sc; ss.time = DateTime.Now; SocketStoreManager.Add(ss); this.sc = sc; Console.WriteLine("接受到了客户端:" + sc.RemoteEndPoint.ToString() + "连接...."); int length = sc.Receive(buffer);//接受客户端握手信息 sc.Send(PackHandShakeData(GetSecKeyAccetp(buffer, length))); sc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), sc); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// 删除 /// </summary> /// <param name="entity"></param> public static void Del(SocketStore entity) { lock (s_lock) { try { if (pool != null) { pool.Remove(entity); } } catch (Exception) { Console.WriteLine("删除池子报错"); } } }
/// <summary> /// 添加 /// </summary> public static void Add(SocketStore entity) { lock (s_lock) { if (pool == null) { pool = new List <SocketStore>(); } //如果池中没有对应的实体,则添加,否则跳过 if (!pool.Exists(m => m.key == entity.key)) { pool.Add(entity); } else { Console.WriteLine("重复实体"); } } }