private static void TestClient() { TcpClient tcpClient = new TcpClient("127.0.0.1", 8001); NetworkStream ns = tcpClient.GetStream(); while (true) { Console.Write("Enter name: "); string msginput = Console.ReadLine(); //构造一个返回的消息 TestHeader header = new TestHeader(); header.msgId = 2; TestBody body = new TestBody(); body.testVal1 = 110; body.testVal2 = msginput; ScadaTCPMsg msg = new ScadaTCPMsg(header, body); ns.Write(msg.MsgBytes, 0, msg.MsgBytes.Length); header.bytes = new byte[8]; int data = ns.Read(header.bytes, 0, 8); if (data > 0) { header.Decode(); PrintUtils.PrintHex(header.bytes); body.BodyBytes = new byte[header.bodyLength]; int bodyLen = ns.Read(body.BodyBytes, 0, header.bodyLength); if (bodyLen > 0) { PrintUtils.PrintHex(body.BodyBytes); } } } }
//这里不是必须重写,如果重写了,服务器处理完消息后,会将此消息再发送给客户端, //也可以手动调用AsyncServer 的Send方法来发送消息 public override ScadaTCPMsg GetSendMsg() { //构造一个返回的消息 TestHeader header = new TestHeader(); header.msgId = 2; TestBody body = new TestBody(); body.testVal1 = 110; body.testVal2 = "Hello," + testVal2; ScadaTCPMsg msg = new ScadaTCPMsg(header, body); msg.CloseClient = false; return(msg); }