private void OnTrainCheckCodeType(TCPEvents arg1, object[] arg2) { textBoxTCPTips.AppendText(arg2.ToString() + "\r\n"); if (arg2 != null && arg2.Length > 0) { Dictionary <string, object> jsonData = arg2[0] as Dictionary <string, object>; if (jsonData != null) { int status = (int)jsonData["status"]; string result = jsonData["result"].ToString(); byte[] data = Convert.FromBase64String(result); MemoryStream ms = new MemoryStream(data); ms.Position = 0; Image graphicPhoto = Image.FromStream(ms); ms.Close(); } else { textBoxTCPTips.AppendText("Error OnTrainCheckCodeType: jsonData is null." + "\r\n"); } } else { textBoxTCPTips.AppendText("Error OnTrainCheckCodeType: arg2" + "\r\n"); } }
private void RecvThread(Socket soc) { Thread th = new Thread((object obj) => { //if (hasThread) return; if (obj != null && obj is Socket) { Socket client = obj as Socket; while (client != null && client.Connected) { try { byte[] cacheBuf = null; byte[] datasize = new byte[1024]; int receiveLength = 0; while ((receiveLength = client.Receive(datasize)) > 0) { //size = BitConverter.ToInt32(datasize, 0); if (cacheBuf == null) { cacheBuf = new byte[receiveLength]; Array.Copy(datasize, cacheBuf, receiveLength); } else { byte[] t = new byte[cacheBuf.Length + receiveLength]; Array.Copy(cacheBuf, t, cacheBuf.Length); Array.Copy(datasize, 0, t, cacheBuf.Length, receiveLength); cacheBuf = t; } if (cacheBuf.Length <= 4) { continue; } int msgl = BitConverter.ToInt32(cacheBuf, 0); while (cacheBuf != null && msgl + 4 <= cacheBuf.Length) { byte[] msgbyte = new byte[msgl]; Array.Copy(cacheBuf, 4, msgbyte, 0, msgl); //test(msgbyte, soc);//拿到完整消息,具体消息操作 string jsonMsg = Encoding.UTF8.GetString(msgbyte); Dictionary <string, object> jsonObject = JsonUtils.Instance.JsonToDictionary(jsonMsg); if (jsonObject != null) { TCPEvents eventType = TCPEvents.None; bool parse = Enum.TryParse <TCPEvents>(jsonObject["code"].ToString(), out eventType); if (parse) { EventManager <TCPEvents> .Instance.TriggerEvent(eventType, jsonObject); } else { textBoxTCPTips.AppendText("Error Code Parse: " + jsonObject["code"].ToString() + "\r\n"); } } else { textBoxTCPTips.AppendText("Error Json Message: " + jsonMsg + "\r\n"); } if (msgl + 4 == cacheBuf.Length) { cacheBuf = null; } else { byte[] tmpByte = new byte[cacheBuf.Length - msgl - 4]; Array.Copy(cacheBuf, msgl + 4, tmpByte, 0, cacheBuf.Length - msgl - 4); cacheBuf = tmpByte; } } } } catch (Exception msg) { Console.WriteLine("Recv Data Thread End.\r\n" + msg.Message, "Error"); break; } } } }); th.Start(soc); }