private void btnResult_Click(object sender, EventArgs e) { #region 客户端发送结果 ShowResult("发送结果"); //发送ready int msgType = MessageType.ClientSendResult; MsgRequestContract contract = new MsgRequestContract("", ""); contract.Key = Guid.NewGuid().ToString(); byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 this.tcpPassiveEngine.PostMessageToServer(reqMessage); #endregion }
private void btnReadOne_Click(object sender, EventArgs e) { //发送readdy请求 string code = tbCode.Text.Trim(); if (string.IsNullOrWhiteSpace(code)) { MessageBox.Show("请输入编码"); return; } ShowResult("发送Ready请求"); //客户端发送ready命令 int msgType = NewMessageType.ClientSendReady; MsgRequestContract contract = new MsgRequestContract("", ""); contract.Key = Guid.NewGuid().ToString(); contract.DeviceCode = code;//设备编码 byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 this.tcpPassiveEngine.PostMessageToServer(reqMessage); }
void tcpPassiveEngine_MessageReceived(System.Net.IPEndPoint serverIPE, byte[] bMsg) { //获取消息类型 int msgType = BitConverter.ToInt32(bMsg, 4);//消息类型是 从offset=4处开始 的一个整数 if (msgType == MessageType.ServerResponseBarCode) { #region 收到服务端返回的条码 MsgResponseContract response = (MsgResponseContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string result = "收到服务端条码:" + response.Msg; this.ShowResult(result); //客户端发送给服务端收到请求 result = "发送收到请求给服务端"; this.ShowResult(result); msgType = MessageType.ClientSendReceive; MsgRequestContract contract = new MsgRequestContract("", ""); contract.Key = Guid.NewGuid().ToString(); byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 this.tcpPassiveEngine.PostMessageToServer(reqMessage); #endregion } if (msgType == MessageType.SeverReceiveResult) { #region 收到服务端返回的收到结果请求 string result = "客户端收到服务端返回结果"; this.ShowResult(result); #endregion } }
private void button1_Click(object sender, EventArgs e) { ShowResult("发送Ready请求"); //发送ready int msgType = MessageType.ClientSendReady; MsgRequestContract contract = new MsgRequestContract("", "MC9K-TR-FCT-1,M2MB999999990,1,M009287,MC9K-TR,,OK,"); contract.Key = Guid.NewGuid().ToString(); byte[] bBody = SerializeHelper.SerializeObject(contract); //消息头 MessageHead head = new MessageHead(bBody.Length, msgType); byte[] bHead = head.ToStream(); //构建请求消息 byte[] reqMessage = new byte[bHead.Length + bBody.Length]; Buffer.BlockCopy(bHead, 0, reqMessage, 0, bHead.Length); Buffer.BlockCopy(bBody, 0, reqMessage, bHead.Length, bBody.Length); //发送请求消息 }
void tcpServerEngine_MessageReceived(IPEndPoint client, byte[] bMsg) { //获取消息类型 int msgType = BitConverter.ToInt32(bMsg, 4);//消息类型是 从offset=4处开始 的一个整数 if (msgType == NewMessageType.ClientSendReady) { #region 客服端发送ready请求,服务端收到ready请求 //改变客户端状态 var clientResult = SystemInfo.ClientInfoList.Where(p => p.Ip == client.Address.ToString()); foreach (var item in SystemInfo.ClientInfoList) { if (item.Ip == client.Address.ToString()) { item.State = 1; } } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendReady的请求"); MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ReceiveClientReady); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); ShowMsghomeControl("向客户端回复" + client.Address.ToString() + "ReceiveClientReady的请求"); #endregion } if (msgType == NewMessageType.ClientSendTestFinished) { #region 客户端发送测试完成 MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendTestFinished的请求," + request.Msg); foreach (var item in SystemInfo.ClientInfoList) { if (item.Ip == client.Address.ToString()) { item.State = 3; } } ClientRquestInfo requestInfo = new ClientRquestInfo(); requestInfo.endPoint = client; requestInfo.messageType = msgType; SystemInfo.clientRequestCQ.Enqueue(requestInfo); //服务端收到客户端测试完成请求 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.ServerReceiveTestFinished); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); MessageResult messageResult = new MessageResult(); if (request.Msg.Split(',')[1].ToUpper() == "PASS") { //放置到good区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } else { //放置到Ng区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } var clientResult = SystemInfo.ClientInfoList.Where(p => p.Ip == client.Address.ToString()); if (clientResult != null && clientResult.FirstOrDefault() != null) { clientResult.FirstOrDefault().MsgResult = messageResult; } ShowMsghomeControl("向客户端回复" + client.Address.ToString() + "ServerReceiveTestFinished的请求"); #endregion } if (msgType == NewMessageType.ClientSendResult) { #region 务端收到客户端发送结果 MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendResult的请求," + request.Msg); ShowMsghomeControl("收到" + client.Address.ToString() + "ClientSendResult的请求," + request.Msg); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, NewMessageType.SeverReceiveResult); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); // ShowMsg("收到客户端测试结果:ClientSendResult:" + request.Msg); MessageResult messageResult = new MessageResult(); if (request.Msg.Split(',')[1].ToUpper() == "PASS") { //放置到good区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } else { //放置到Ng区 messageResult.BarCode = request.Msg.Split(',')[0]; messageResult.Code = request.Msg.Split(',')[1].ToUpper(); } var clientResult = SystemInfo.ClientInfoList.Where(p => p.Ip == client.Address.ToString()); if (clientResult != null && clientResult.FirstOrDefault() != null) { clientResult.FirstOrDefault().MsgResult = messageResult; } ShowMsghomeControl("向客户端回复:" + client.Address.ToString() + "SeverReceiveResult的请求,"); #endregion } }
void tcpServerEngine_MessageReceived(IPEndPoint client, byte[] bMsg) { //获取消息类型 int msgType = BitConverter.ToInt32(bMsg, 4);//消息类型是 从offset=4处开始 的一个整数 if (msgType == MessageType.ClientSendReady) { #region 收到客户端发送的ready请求,返回条码给客户端 var curDevInfo = clientSendList.Where(p => p.IP == client.Address.ToString()); if (curDevInfo != null && curDevInfo.FirstOrDefault() != null) { clientSendList.Remove(curDevInfo.FirstOrDefault()); } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string record = "收到客户端ready请求:"; this.ShowClientMsg(client, record); record = "返回条码12345678到客户端"; this.ShowClientMsg(client, record); ClientSendInfo sendInfo = new ClientSendInfo(); sendInfo.IP = client.Address.ToString(); sendInfo.SendBarCodeTime = DateTime.Now; sendInfo.IpPoint = client; var curDevInfoResult = AppInfo.ClientInfoList.Where(p => p.IP == sendInfo.IP); if (curDevInfoResult != null && curDevInfoResult.FirstOrDefault() != null) { sendInfo.Name = curDevInfoResult.FirstOrDefault().Name; sendInfo.Code = curDevInfoResult.FirstOrDefault().Code; } sendInfo.BarCode = "12345678"; clientSendList.Add(sendInfo); //回复消息体 MsgResponseContract response = new MsgResponseContract("", "12345678"); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, MessageType.ServerResponseBarCode); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); #endregion } if (msgType == MessageType.ClientSendReceive) { #region 务端收到客户已经接受的请求 var curDevInfo = clientSendList.Where(p => p.IP == client.Address.ToString()); if (curDevInfo != null && curDevInfo.FirstOrDefault() != null) { clientSendList.Remove(curDevInfo.FirstOrDefault()); } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string record = "收到客户端receive请求:"; this.ShowClientMsg(client, record); ClientSendInfo sendInfo = new ClientSendInfo(); sendInfo.IP = client.Address.ToString(); sendInfo.ReceiveTime = DateTime.Now; sendInfo.IpPoint = client; var curDevInfoResult = AppInfo.ClientInfoList.Where(p => p.IP == sendInfo.IP); if (curDevInfoResult != null && curDevInfoResult.FirstOrDefault() != null) { sendInfo.Name = curDevInfoResult.FirstOrDefault().Name; sendInfo.Code = curDevInfoResult.FirstOrDefault().Code; } sendInfo.BarCode = ""; clientSendList.Add(sendInfo); #endregion } if (msgType == MessageType.ClientSendResult) { #region 务端收到客户端发送结果 var curDevInfo = clientSendList.Where(p => p.IP == client.Address.ToString()); if (curDevInfo != null && curDevInfo.FirstOrDefault() != null) { clientSendList.Remove(curDevInfo.FirstOrDefault()); } MsgRequestContract request = (MsgRequestContract)SerializeHelper.DeserializeBytes(bMsg, MessageHead.HeadLength, bMsg.Length - MessageHead.HeadLength); string record = "收到客户端发送结果:"; this.ShowClientMsg(client, record); record = "发送收到结果消息给客户端:"; this.ShowClientMsg(client, record); //回复消息体 MsgResponseContract response = new MsgResponseContract("", ""); response.Key = request.Key; byte[] bReponse = SerializeHelper.SerializeObject(response); //回复消息头 MessageHead head = new MessageHead(bReponse.Length, MessageType.SeverReceiveResult); byte[] bHead = head.ToStream(); //构建回复消息 byte[] resMessage = new byte[bHead.Length + bReponse.Length]; Buffer.BlockCopy(bHead, 0, resMessage, 0, bHead.Length); Buffer.BlockCopy(bReponse, 0, resMessage, bHead.Length, bReponse.Length); //发送回复消息 this.tcpServerEngine.PostMessageToClient(client, resMessage); #endregion } }