예제 #1
0
        /// <summary>
        /// 向服务端发送消息,并接收消息
        /// </summary>
        /// <param name="protocol">协议对象</param>
        /// <returns></returns>
        public static MessageProtocol SendAndReceiveMessage(MessageProtocol protocol)
        {
            if (mc == null)
            {
                mc = new MessageClient(_ip, _port);
            }

            MessageProtocol _protocol = null;

            try
            {
                if (!protocol.SerialNumberLock)
                {
                    protocol.SerialNumber = serialNumber.ToString();
                    //流水号递增
                    serialNumber++;
                    if (serialNumber > 1000000)
                    {
                        serialNumber = 0;
                    }
                }
                if (mc.Connect())
                {
                    //发送命令
                    string message = ProtocolTranslator.SerilizeMessage(protocol);
                    mc.Send(message);

                    Thread.Sleep(100);
                    string str = mc.Receive(ProtocolTranslator.StartFlag, ProtocolTranslator.EndFlag);
                    _protocol = ProtocolTranslator.DeserilizeMessage(str);
                }
            }
            catch (Exception ex)
            {
                Log.writeLineToLog(ex.Message, "云支撑平台文件通讯故障");
            }
            return(_protocol);
        }