protected void AcceptedClientCallBack(IAsyncResult result) { try { TcpListener listener = (TcpListener)result.AsyncState; TcpClient client = listener.EndAcceptTcpClient(result); TCPClientBase tcpClientBase = TCPClientBase.GetInstance(client, this.BufferSize); lock (_lockObject) { if (this.Clients == null) { this.Clients = new List <TCPClientBase>(); } if (!this.Clients.Contains(tcpClientBase)) { this.Clients.Add(tcpClientBase); } } tcpClientBase.NetworkStream.Flush(); tcpClientBase.NetworkStream.BeginRead(tcpClientBase.CacheSync, 0, tcpClientBase.CacheSync.Length, ReadCallBack, tcpClientBase); if (IsRunning) { listener.BeginAcceptTcpClient(AcceptedClientCallBack, result.AsyncState); } } catch (Exception ex) { throw ex; } finally { } }
void ConnectCallBack(IAsyncResult result) { TcpClient client = (TcpClient)result.AsyncState; try { _client = TCPClientBase.GetInstance(client, this.BuffSize); client.EndConnect(result); IsConnected = true; //处理连接事件 未完成 //client.GetStream().BeginRead(_client.CacheAsync } catch (Exception ex) { } finally { } }