//数据处理接口 public void ClientServer() { try { while (true) { Socket sokConnection = socketWatch.Accept(); Console.WriteLine(DateTime.Now + "_连接成功_" + sokConnection.RemoteEndPoint); LogHelper.Instance.AddLog("日志", "记录", "连接成功_" + sokConnection.RemoteEndPoint); Byte[] sendBytes = ArConvert.strToToHexByte("21808500"); Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "Send: Msg:{0}", "21808500"); LogHelper.Instance.AddLog("日志", "记录", "Send: Msg:{0}" + "21808500"); sokConnection.Send(sendBytes, 0); Thread thr = new Thread(RecMsg); thr.IsBackground = true; thr.Start(sokConnection); dictThread.Add(sokConnection.RemoteEndPoint.ToString(), thr); // 将新建的线程 添加 到线程的集合中去。 } } catch (Exception ex) { Console.Write("出现异常:" + ex.ToString()); LogHelper.Instance.AddLog("日志", "异常", "出现异常:" + ex.ToString()); //Console.ReadLine(); } }
/// <summary> /// 发送函数 /// </summary> /// <param name="strCode"></param> /// <param name="strMsg"></param> /// <returns></returns> public Boolean SendMsg(String strCode, String strMsg, string strMsgCode) { try { if (String.IsNullOrEmpty(strCode) == true || string.IsNullOrEmpty(strMsg) == true) { return(false); } if (this.priClientList.Any(m => m.Code.Equals(strCode) == true && m.RefreshTime.AddMinutes(10) >= DateTime.Now) == false) { return(false); } ClientEntity clientEnt = this.priClientList.FirstOrDefault(m => m.Code.Equals(strCode) == true); clientEnt.MsgCode = strMsgCode; if (strMsg.Length >= 8) { clientEnt.CMD = strMsg.Substring(0, 8); } Byte[] sendBytes = ArConvert.strToToHexByte(strMsg); Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "Send: Code:{0},Msg:{1}", strCode, strMsg); LogHelper.Instance.AddLog("日志", "记录", String.Format("Send: Code:{0},Msg:{1}", strCode, strMsg)); //LogHelper.Instance.AddLog("日志", "服务端(SendMsg)", string.Format("Send: Code:{0},Msg:{1}", strCode, strMsg)); Boolean bjg = clientEnt.ClientSoket.Send(sendBytes, 0) > 0 ? true : false; //clientEnt.ClientSoket.Close(); //clientEnt.ClientSoket.co return(bjg); } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "异常" + "服务端(SendMsg)" + ex.Message); LogHelper.Instance.AddLog("日志", "异常", "服务端(SendMsg)" + ex.Message); //LogHelper.Instance.AddLog("错误", "服务端(SendMsg)", ex.Message); } return(false); }