void ListenThread()//监听线程 { Listener.Start(); try { while (listening) { if (Listener.Pending()) { var td = new Thread(ClientThread) { IsBackground = true }; var ct = new ClientItem { Client = Listener.AcceptTcpClient() }; ClientTable.Add(ct); td.Start(ct); } Thread.Sleep(Interval); } } finally { Listener.Stop(); OnEndListen?.Invoke(); } }
private void ListenThread() { IPEndPoint remoted = null; try { while (_listening) { if (_listener.Available > 0) { var bytes = _listener.Receive(ref remoted); OnReceive?.Invoke(new UdpReceiveResult(bytes, remoted)); } else { Thread.Sleep(Interval); } } } finally { _listener.Close(); _listening = false; OnEndListen?.Invoke(); } }