Exemple #1
0
        /// <summary>
        /// 处理接收数据,数据来自客户端SocketAsyncEventArgs对象
        /// </summary>
        /// <param name="receiveEventArgs"></param>
        private void TCPServerProcessReceive(SocketAsyncEventArgs receiveEventArgs)
        {
            if (receiveEventArgs.SocketError != SocketError.Success)
            {
                TCPServerCloseClientSocket(receiveEventArgs);
            }

            SocketUserToKen _SocketUserToKen = receiveEventArgs.UserToken as SocketUserToKen;

            /* 需要处理的字节数 */
            int remainingBytesToProcess = receiveEventArgs.BytesTransferred;

            if (remainingBytesToProcess > 0)
            {
                bool _ReceivePending = _SocketUserToKen.Socket.ReceiveAsync(receiveEventArgs);

                if (!_ReceivePending)
                {
                    TCPServerProcessReceive(receiveEventArgs);   /* 递归调用,直到receiveSocket上没有数据为止(全部接收完毕) */
                }
            }
            else
            {
                TCPServerCloseClientSocket(receiveEventArgs);   /* 客户端已经关闭连接 */
            }
        }
Exemple #2
0
        /// <summary>
        /// 关闭客户端SocketAsyncEventArgs对象
        /// </summary>
        /// <param name="acceptEventArgs"></param>
        private void TCPServerCloseClientSocket(SocketAsyncEventArgs acceptEventArgs)
        {
            SocketUserToKen _SocketUserToKen = acceptEventArgs.UserToken as SocketUserToKen;

            /* 在连接区移除客户端的IP结点信息 */

            _SocketUserToKen.Socket.Close();
            _TCPServerSocketAsyncEventArgsPool.Push(acceptEventArgs);
            TCPServerSemaphoreConnections.Release();
        }
Exemple #3
0
        /// <summary>
        /// 处理发送数据,数据发送给客户端SocketAsyncEventArgs对象
        /// </summary>
        /// <param name="sendEventArgs"></param>
        private void TCPServerProcessSend(SocketAsyncEventArgs sendEventArgs)
        {
            if (sendEventArgs.SocketError != SocketError.Success)
            {
                TCPServerCloseClientSocket(sendEventArgs);
            }

            SocketUserToKen _SocketUserToKen = sendEventArgs.UserToken as SocketUserToKen;

            bool _SendPending = _SocketUserToKen.Socket.SendAsync(sendEventArgs);

            if (!_SendPending)
            {
                TCPServerProcessSend(sendEventArgs);   /* 递归调用,直到sendSocket上没有数据为止(全部发送完毕) */
            }
        }