//组包接收 public bool receiveMsgByPackages(ref REVMSGBYPACKAGES resMsg, INetRouterClient client) { int flag = 0; int packNum = 1; //包数 List <string> rsvPckgs = new List <string>(); List <int> packOrderList = new List <int>(); REVMSG recvMsg = new REVMSG(); while (flag < packNum) { while (client.receiveMessage(ref recvMsg)) { byte[] sbrsv = recvMsg.msg; packNum = (int)sbrsv[0]; int order = (int)sbrsv[1]; packOrderList.Add(order); int len = (int)(((sbrsv[2] & 0xff) << 8) | (sbrsv[3] & 0xff)); byte[] sbtemp = new byte[len]; for (int j = 4; j < len + 4; j++) { sbtemp[j - 4] = sbrsv[j]; } rsvPckgs.Add(System.Text.Encoding.Default.GetString(sbtemp)); ++flag; } } if (flag == packNum) { int i = 0; StringBuilder sbres = new StringBuilder(); while (i < flag) { for (int j = 0; j < flag; j++) { if (packOrderList[j] == i) { sbres.Append(rsvPckgs[j]); } } ++i; } resMsg.msg = sbres.ToString();//接收字符串长度不超过65535? return(true); } else { return(false); } }
static void Main(string[] args) { NETADDR localaddr = new NETADDR(); localaddr.bureauCode = 8; localaddr.unitType = 2; localaddr.unitId = 0; localaddr.devType = 1; localaddr.devId = 6; INetRouterClient netRouterClient = NetRouterClientFactory.CreateNetRouterClient("Test", "172.168.0.1", 9003, "172.168.0.1", 9005, ref localaddr, ""); while (!netRouterClient.start()) { Console.Write("Start fails.\n"); Thread.Sleep(10); } Console.Write("Start succeeds.\n"); REVMSG recvMsg = new REVMSG(); while (true) { while (netRouterClient.receiveMessage(ref recvMsg)) { string revmsgTemp = System.Text.Encoding.Default.GetString(recvMsg.msg, 0, 5); Console.WriteLine(revmsgTemp); } } Console.ReadKey(); }
private void ReceiveThreadMethod() { while (!m_ThreadClose) { try { if (this.m_NetRouterClient != null) { if (this.m_ConnectStatus1 || this.m_ConnectStatus2) { REVMSG recvMsg = new REVMSG(); while (m_NetRouterClient.receiveMessage(ref recvMsg)) { byte[] t_ReceiveData = recvMsg.msg; if (t_ReceiveData != null) { int t_DataLength = ByteToInt32(t_ReceiveData); bool t_NeedOperate = true; if (t_DataLength == 4) { if (t_ReceiveData[4] == m_HeartBeatData[0] && t_ReceiveData[5] == m_HeartBeatData[1] && t_ReceiveData[6] == m_HeartBeatData[2] && t_ReceiveData[7] == m_HeartBeatData[3]) { t_NeedOperate = false; DateTime dt = DateTime.Now; if (this.m_StatusStrAction != null) { this.m_StatusStrAction(dt.ToLongTimeString() + " 收到心跳信息包"); } if (m_ifReplyHeartBeat) { SendHeartBeatReply(recvMsg.srcAddr); } } else if (t_ReceiveData[4] == m_HeartBeatReply[0] && t_ReceiveData[5] == m_HeartBeatReply[1] && t_ReceiveData[6] == m_HeartBeatReply[2] && t_ReceiveData[7] == m_HeartBeatReply[3]) { t_NeedOperate = false; DateTime dt = DateTime.Now; if (this.m_StatusStrAction != null) { this.m_StatusStrAction(dt.ToLongTimeString() + " 收到心跳信息应答包"); } NETADDR t_SrcAddr = recvMsg.srcAddr; string t_AddrStr = NetRouterClientHelper.ConvertAddress(t_SrcAddr); EndPointRecord t_EndPoint; if (this.m_RemoteAddressMap.TryGetValue(t_AddrStr, out t_EndPoint)) { t_EndPoint.m_HeartBeatInterval = 0; } } } if (t_NeedOperate && this.m_ReceiveAction != null) { this.m_ReceiveAction(t_ReceiveData, 4, t_DataLength); } } } } } Thread.Sleep(10); } catch (ThreadAbortException e) { System.Console.WriteLine(Thread.CurrentThread.Name + "已被要求退出"); Thread.ResetAbort(); } catch (Exception e) { if (this.m_StatusStrAction != null) { this.m_StatusStrAction(e.Message); } } } }
static void Main(string[] args) { NETADDR localaddr = new NETADDR(); localaddr.bureauCode = 8; localaddr.unitType = 2; localaddr.unitId = 0; localaddr.devType = 1; localaddr.devId = 6; INetRouterClient netRouterClient = NetRouterClientFactory.CreateNetRouterClient("Test", "191.168.99.98", 9003, "10.2.48.123", 9005, ref localaddr, ""); while (!netRouterClient.start()) { Console.Write("Start fails.\n"); Thread.Sleep(10); } Console.Write("Start succeeds.\n"); while (true) { if (netRouterClient.isNet1Connected() || netRouterClient.isNet2Connected()) { if (SENDNUM + 1 < SENDNUM) { SENDNUM = 0; } REVMSG recvMsg = new REVMSG(); List <SENDMSG> sendlist = new List <SENDMSG>(); while (netRouterClient.receiveMessage(ref recvMsg)) { string revmsgTemp = System.Text.Encoding.Default.GetString(recvMsg.msg, 0, recvMsg.msg.Length); Console.WriteLine("Rev len : " + recvMsg.msg.Length); Console.WriteLine("Rev (" + recvMsg.srcAddr.unitId + ":" + recvMsg.srcAddr.devId + ")---" + dataNum(revmsgTemp)); //Console.WriteLine("Rev (" + recvMsg.srcAddr.unitId + ":" // + recvMsg.srcAddr.devId + ")---" // + recvMsg.msg); String ack = (++SENDNUM).ToString(); ack += " Hello!"; //byte[] tempByte = Encoding.Default.GetBytes(ack); List <NETADDR> adrlist = new List <NETADDR>(); adrlist.Add(recvMsg.srcAddr); SENDMSG sMessage = new SENDMSG(ref ack, ref adrlist); sendlist.Add(sMessage); if (!netRouterClient.sendMessage(ref sMessage)) { Console.Write("Send Wrong!\n"); } else { Console.WriteLine("Send -------- " + ack + " : " + ack.ToString().Length + "\n"); } } } try { Thread.Sleep(10); } catch (Exception e) { // TODO Auto-generated catch block Console.Write(e.Message + "\n"); } } }