예제 #1
0
파일: WebManage.cs 프로젝트: tinshen/pidDev
        /// <summary>
        /// 向PID发送指令并获取返回结果信息
        /// </summary>
        /// <param name="cmdContent">指令信息</param>
        /// <param name="OfficeCode">指定操作Office</param>
        /// <param name="PnFlag">是否最后一个指令自动PN(true:自动PN;false:不PN)</param>
        /// <param name="AllResult">是否返回所有结果信息(true:返回所有指令结果;false:只返回最后一个指令的结果)</param>
        /// <returns>结果信息</returns>
        public static string SendCommand(string cmdContent, string OfficeCode, bool PnFlag, bool AllResult)
        {
            string tmpByData = cmdContent;

            try
            {
                if (OfficeCode.Trim() != "")
                {
                    tmpByData += "<OFFICE>" + OfficeCode.Trim() + "</OFFICE>";
                }

                if (PnFlag)
                {
                    tmpByData += "<PN>1</PN>";
                }
                else
                {
                    tmpByData += "<PN>0</PN>";
                }

                if (AllResult)
                {
                    tmpByData += "<ALL>1</ALL>";
                }
                else
                {
                    tmpByData += "<ALL>0</ALL>";
                }

                string ReturnValue = "";

                //读取Web.Config配置中的服务端IP地址
                string IPAddress = m_PidServerIP;
                //读取Web.Config配置中的服务端端口
                int Port = m_PidServerPort;

                //读取Web.Config配置中的用户名
                string User = m_WebUser;
                //读取Web.Config配置中的密码
                string PWD = AppGlobal.EncryptMD5(m_WebPwd);

                //底层通讯协议
                //数据包格式:包头+类型+长度+内容+包尾
                //包头起始(2字节)0xFF,0xFE
                //类型(1字节)0x01
                //总长度(2字节)
                //用户名(10字节)
                //密码(32字节)
                //内容()
                //包尾结束(2字节)0xFF,0xFF

                TcpClient client;           //创建Tcp网络服务

                short wLength = 26;         // 整个数据包长度
                short wCmdLen = 0;          // 命令数据包长度

                byte byType = 0x01;

                //计算需要发送信息的二进制流长度
                wCmdLen = (short)((System.Text.Encoding.Default.GetBytes(tmpByData)).Length);

                wLength = (short)(wCmdLen + 39 + User.Length);

                wLength = System.Net.IPAddress.HostToNetworkOrder(wLength);

                ArrayList al = new ArrayList();
                al.AddRange(m_WebHeadPack);                                  //报头长度2
                al.Add(byType);                                              //长度1
                al.AddRange(BitConverter.GetBytes(wLength));                 //长度2
                al.AddRange(AppGlobal.GetBin(AppGlobal.StrToHex(User), 10)); //长度10
                al.AddRange(AppGlobal.GetBin(AppGlobal.StrToHex(PWD), 32));  //长度32
                al.AddRange(AppGlobal.GetBin(AppGlobal.StrToHex(tmpByData), tmpByData.Length));
                al.AddRange(m_WebEndPack);

                byte[] SendBytes = (byte[])al.ToArray(typeof(byte));

                try
                {
                    client = new TcpClient(IPAddress, Port);
                }
                catch (SocketException ex)
                {
                    ReturnValue = ex.Message;
                    return(ReturnValue);
                }

                NetworkStream ns = client.GetStream();
                ns.Write(SendBytes, 0, SendBytes.Length);
                ns.Flush();

                byte[] data = new byte[1024 * 1024];

                int recv = ns.Read(data, 0, data.Length);
                if (recv == 0)
                {
                    ReturnValue = "";
                }
                else
                {
                    //检查数据包是否完整
                    if ((data[0] == m_WebHeadPack[0]) && (data[1] == m_WebHeadPack[1]) &&
                        (data[recv - 2] == m_WebEndPack[0]) && (data[recv - 1] == m_WebEndPack[1]))
                    {
                        byte[] tmpdata2 = new byte[recv - 4];
                        Array.Copy(data, 2, tmpdata2, 0, recv - 4);
                        //转换并解压缩
                        //Kevin 2010-12-20 Add
                        //添加数据包压缩、解压缩
                        ReturnValue = System.Text.Encoding.Default.GetString(tmpdata2);//AppGlobal.Decompress(System.Text.Encoding.Default.GetString(tmpdata2));
                        ReturnValue = ReturnValue.Replace('\r', '^');
                    }
                    else
                    {
                        ReturnValue = "接收到的数据包不完整!";
                    }
                }


                ns.Close();
                client.Close();

                return(ReturnValue);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }