/// <summary> /// Ends the accept connect. /// </summary> /// <param name="iar">The iar.</param> private void EndAcceptConnect(IAsyncResult iar) { //接受一个客户端的连接请求 try { Socket oldserver = (Socket)iar.AsyncState; Socket client = oldserver.EndAccept(iar); T TSession = new T() { AsyncSocket = client }; TSession.OnAsyncDisconnect += new EventHandler <AsyncEventArgs <T> >( delegate(object sender, AsyncEventArgs <T> e) { if (this.OnTSessionClosed != null) { this.OnTSessionClosed(sender, e); } Close(e.Session); } ); TSession.OnAsynConnect += new EventHandler <AsyncEventArgs <T> >( delegate(object sender, AsyncEventArgs <T> e) { if (this.OnTSessionAccept != null) { this.OnTSessionAccept(sender, e); } } ); TSession.OnReadPacket += new EventHandler <AsyncEventArgs <P, P, T> >( delegate(object sender, AsyncEventArgs <P, P, T> e) { FireOnReadPacket(e); } ); TSession.OnPacketSent += new EventHandler <AsyncEventArgs <P, P, T> >( delegate(object sender, AsyncEventArgs <P, P, T> e) { if (this.OnPacketSent != null) { this.OnPacketSent(sender, e); } } ); if ((this.MaxSession ?? 50) <= this.TSessionCount) { if (this.OnTSessionFull != null) { this.OnTSessionFull(TSession, new AsyncEventArgs <T>(TSession)); } return; } TSession.Connect();//开始读取线程 lock (__TSessionCollection) { this.__TSessionCollection.Add(TSession); } //继续接受客户端 AsyncSocket.BeginAccept(new AsyncCallback(EndAcceptConnect), AsyncSocket); } catch { return; } }