示例#1
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                AcceptStateObject state = (AcceptStateObject)ar.AsyncState;
                // Get the socket that handles the client request.
                Socket listener = state.workSocket;
                Socket handler  = listener.EndAccept(ar);

                SockBase sockBase = new SockBase(handler, SocketRole.Client, true);
                sockBase.IsConnected = true;
                sockBase.StartReceive();

                SocketAcceptEvent?.Invoke(this, new SocketAcceptEventArgs(state, sockBase));
                if (state.externalCallback != null)
                {
                    state.externalCallback(this, new SocketAcceptEventArgs(state, sockBase));
                }

                listener.BeginAccept(
                    new System.AsyncCallback(AcceptCallback), state);
            }
            catch (ObjectDisposedException) { }  // listener closed
        }