Пример #1
0
        public void ProcessReceive(SocketAsyncEventArgs e)
        {
            UserToken token = e.UserToken as UserToken;

            //判断网络消息接收是否成功
            if (token.ReceiveSAEA.BytesTransferred > 0 && token.ReceiveSAEA.SocketError == SocketError.Success)
            {
                byte[] message = new byte[token.ReceiveSAEA.BytesTransferred];
                Buffer.BlockCopy(token.ReceiveSAEA.Buffer, 0, message, 0, token.ReceiveSAEA.BytesTransferred);
                //处理接收到的消息
                token.Receive(message);
                StartReceive(token);
            }
            else
            {
                if (token.ReceiveSAEA.SocketError != SocketError.Success)
                {
                    ClientClose(token, token.ReceiveSAEA.SocketError.ToString());
                }
                else
                {
                    //消息长度为0就认为客户端主动断开连接
                    ClientClose(token, "客户端主动断开连接");
                }
            }
        }
Пример #2
0
        public void ProcessReceive(SocketAsyncEventArgs e)
        {
            UserToken token = pool.Pop(); token.conn = sendSocket; token.SendSAEA.RemoteEndPoint = e.RemoteEndPoint;

            //判断网络消息接收是否成功
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                byte[] message = new byte[e.BytesTransferred];
                Buffer.BlockCopy(e.Buffer, 0, message, 0, e.BytesTransferred);
                //处理接收到的消息
                token.Receive(message);
                StartReceive();
            }
            else
            {
                if (e.SocketError != SocketError.Success)
                {
                    ClientClose(token, e.SocketError.ToString());
                }
                else
                {
                    //消息长度为0就认为客户端主动断开连接
                    ClientClose(token, "客户端主动断开连接");
                }
            }
        }