Beispiel #1
0
        /// <summary>
        /// 处理接受的请求
        /// </summary>
        /// <param name="e"></param>
        private void processReceive(SocketAsyncEventArgs e)
        {
            ClientPeer client = e.UserToken as ClientPeer;

            //判断网络消息是否接受成功
            if (client.ReceiveArgs.SocketError == SocketError.Success && client.ReceiveArgs.BytesTransferred > 0)
            {
                //拷贝数据到数组中
                byte[] packet = new byte[client.ReceiveArgs.BytesTransferred];
                Buffer.BlockCopy(client.ReceiveArgs.Buffer, 0, packet, 0, client.ReceiveArgs.BytesTransferred);
                //让客户端自身处理这个数据包 自身解析
                client.StartReceive(packet);
                //尾递归
                startReceive(client);
            }
            //断开连接了 如果没有传输的字节数 就代表断开连接了
            else if (client.ReceiveArgs.BytesTransferred == 0)
            {
                if (client.ReceiveArgs.SocketError == SocketError.Success)
                {
                    //客户端主动断开连接
                    Disconnect(client, "客户端主动断开连接");
                }
                else
                {
                    //由于网络异常 导致 被动断开连接
                    Disconnect(client, client.ReceiveArgs.SocketError.ToString());
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 处理客户端发送的数据
        /// </summary>
        /// <param name="receiveArgs"></param>
        private void ProcessReceive(SocketAsyncEventArgs receiveArgs)
        {
            ClientPeer client = receiveArgs.UserToken as ClientPeer;

            if (client.ReceiveArgs.SocketError == SocketError.Success && client.ReceiveArgs.BytesTransferred > 0)
            {
                byte[] dataArray = new byte[client.ReceiveArgs.BytesTransferred];
                Buffer.BlockCopy(client.ReceiveArgs.Buffer, 0, dataArray, 0, client.ReceiveArgs.BytesTransferred);

                client.StartReceive(dataArray); // 拆包

                StartReceive(client);           // 尾递归
            }
            else if (client.ReceiveArgs.BytesTransferred == 0)
            {
                if (client.ReceiveArgs.SocketError == SocketError.Success)
                {
                    // 客户端主动断开连接
                    Disconnect(client, "客户端主动断开连接");
                }
                else
                {
                    // 网络异常 , 被动断开连接
                    Disconnect(client, client.ReceiveArgs.SocketError.ToString());
                }
            }
        }
Beispiel #3
0
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            ClientPeer client = e.UserToken as ClientPeer;  //为什么这里不用e.acceptSocket

            ///判断网络消息是否接收成功
            if (client.ReceiveArgs.SocketError == SocketError.Success && client.ReceiveArgs.BytesTransferred > 0)
            {
                //拷贝到数组中
                byte[] packet = new byte[client.ReceiveArgs.BytesTransferred];
                Buffer.BlockCopy(client.ReceiveArgs.Buffer, 0, packet, 0, client.ReceiveArgs.BytesTransferred);

                client.StartReceive(packet);

                StartReceive(client);
            }
            //断开连接   //无传输数据表示断开连接
            else if (client.ReceiveArgs.BytesTransferred == 0)
            {
                if (client.ReceiveArgs.SocketError == SocketError.Success)
                {
                    //客户端主动断开连接
                    DisConnect(client, "客户端主动断开连接");
                }
                else
                {
                    //由于网络异常 被动断开
                    DisConnect(client, client.ReceiveArgs.SocketError.ToString());
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 处理接收的请求
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            ClientPeer client = e.UserToken as ClientPeer;   //userToken  获得与此异步套接字操作相关的用户或应用程序

            //判断网络消息是否接收成功
            if (client.receiveArgs.SocketError == SocketError.Success && client.receiveArgs.BytesTransferred > 0)
            {
                //拷贝数据
                byte[] packet = new byte[client.receiveArgs.BytesTransferred];
                Buffer.BlockCopy(client.receiveArgs.Buffer, 0, packet, 0, client.receiveArgs.BytesTransferred);
                //客户端自身处理数据包
                client.StartReceive(packet);
                //尾递归
                StartReceive(client);
            }
            //断开连接  没有传输的字节数
            else if (client.receiveArgs.BytesTransferred == 0)
            {
                //客户端主动断开
                if (client.receiveArgs.SocketError == SocketError.Success)
                {
                    DisConnectd(client, "客户端主动断开");
                }
                //网络异常导致
                else
                {
                    DisConnectd(client, e.SocketError.ToString());
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// 处理接收的请求
        /// </summary>
        /// <param name="e"></param>
        private void processReceive(SocketAsyncEventArgs e)
        {
            //反方向获得对象,因为这里是Compeleted 获得的对象,那个方法又被指定了对象,所以就限制了方向

            ClientPeer client = e.UserToken as ClientPeer;

            //
            if (client.ReceiveArgs.SocketError == SocketError.Success && client.ReceiveArgs.BytesTransferred > 0)
            {
                //Console.WriteLine("数据符合逻辑");
                //获取收据的长度
                byte[] packet = new byte[client.ReceiveArgs.BytesTransferred];
                //拷贝到数组中
                Buffer.BlockCopy(client.ReceiveArgs.Buffer, 0, packet, 0, client.ReceiveArgs.BytesTransferred);
                //客户端自身解释
                client.StartReceive(packet);
                //解释完成以后,又得开启接收 (C# 可能就是这样,就收后流可能要重新设置)
                startReceive(client);
            }
            else
            {
                //如果是进入else ,那么就是网络失败了,
                //如果传输的数据为0
                if (client.ReceiveArgs.BytesTransferred == 0)
                {
                    //
                    if (client.ReceiveArgs.SocketError == SocketError.Success)
                    {
                        // 如果是正常,那么就是 Socket Client 主动断开连接
                        DisConnect(client, "客户端主动断开连接");
                    }
                    else
                    {
                        //否则就是网络异常断开连接
                        DisConnect(client, client.ReceiveArgs.SocketError.ToString());
                    }
                }
            }
        }