private static MessageProtocol ReceiveMessage() { MessageProtocol _protocol = null; if (ServiceTest()) { //接收数据 string str = mc.Receive(ProtocolTranslator.StartFlag, ProtocolTranslator.EndFlag); if (str.Length > 0) { _protocol = ProtocolTranslator.DeserilizeMessage(str); } } return(_protocol); }
private static MessageProtocol ReceiveMessage() { if (mc == null) { return(null); } //接收数据 string str = mc.Receive(ProtocolTranslator.StartFlag, ProtocolTranslator.EndFlag); MessageProtocol _protocol = null; if (str.Length > 0) { _protocol = ProtocolTranslator.DeserilizeMessage(str); } return(_protocol); }
/// <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); }