コード例 #1
0
        /// <summary>
        /// 处理终端异步连接
        /// </summary>
        /// <param name="asyncResult">IAsyncResult</param>
        /// 时间:2016/6/7 22:54
        /// 备注:
        internal void ClientConnected(IAsyncResult asyncResult)
        {
            Interlocked.Increment(ref currentConnections);
            SocketConnectionInfo _connection = new SocketConnectionInfo
            {
                Buffer = new byte[SocketConnectionInfo.BufferSize]
            };
            Socket _asyncListener = (Socket)asyncResult.AsyncState;
            Socket _asyncClient   = _asyncListener.EndAccept(asyncResult);

            _connection.Socket = _asyncClient;
            OnClientConnected.RaiseEvent(null, CreateSocketSeesion(_connection, null));

            if (this.Protocol == SocketProtocol.TCP)
            {
                _asyncClient.BeginReceive(_connection.Buffer, 0, _connection.Buffer.Length, SocketFlags.None, new AsyncCallback(DataReceived), _connection);
            }
            else if (this.Protocol == SocketProtocol.UDP)
            {
                _asyncClient.BeginReceiveFrom(_connection.Buffer, 0, _connection.Buffer.Length, SocketFlags.None, ref ipeSender, new AsyncCallback(DataReceived), _connection);
            }

            listener.BeginAccept(new AsyncCallback(ClientConnected), _connection);
        }