Пример #1
0
        /// <summary>
        /// 接收指令
        /// </summary>
        /// <returns></returns>
        public bool RecvCommand()
        {
            m_recvBuffer.Clear();
            m_tcpClient.Client.Receive(m_recvBuffer.Buffer, sizeof(int), SocketFlags.None);
            int packetLength = BitConverter.ToInt32(m_recvBuffer.Buffer, 0); //获取包长度

            if (NetByteOrder)
            {
                packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength);   //把网络字节顺序转为本地字节顺序
            }
            m_recvBuffer.SetBufferSize(sizeof(int) + packetLength);                     //保证接收有足够的空间
            m_tcpClient.Client.Receive(m_recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None);
            int    commandLen = BitConverter.ToInt32(m_recvBuffer.Buffer, sizeof(int)); //取出命令长度
            string tmpStr     = Encoding.UTF8.GetString(m_recvBuffer.Buffer, sizeof(int) + sizeof(int), commandLen);

            //接收到以后
            return(true);
        }
Пример #2
0
        public bool RecvCommand()
        {
            recvBuffer.Clear();
            tcpClient.Client.Receive(recvBuffer.Buffer, sizeof(int), SocketFlags.None);
            int packetLength = BitConverter.ToInt32(recvBuffer.Buffer, 0); //获取包长度

            if (NetByteOrder)
            {
                packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序
            }
            recvBuffer.SetBufferSize(sizeof(int) + packetLength);                     //保证接收有足够的空间
            tcpClient.Client.Receive(recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None);
            int    commandLen = BitConverter.ToInt32(recvBuffer.Buffer, sizeof(int)); //取出命令长度
            string tmpStr     = Encoding.UTF8.GetString(recvBuffer.Buffer, sizeof(int) + sizeof(int), commandLen);

            if (!incomingDataParser.DecodeProtocolText(tmpStr)) //解析命令
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }