コード例 #1
0
        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);
                    }
                }
            }
        }
コード例 #2
0
        private static void MainHandler(ScadaTCPHeader header, ScadaTCPBody body)
        {
            //这里一般在header中可能有消息ID。可以在这里进行区分
            TestBody   test       = (TestBody)body;
            TestHeader testHeader = (TestHeader)header;

            LOG.Info("我去,这里竟然回调了" + test.testVal1 + "," + test.testVal2);
            LOG.InfoFormat("回调的MsgId:{0},bodyLen:{1},from:{2}", testHeader.msgId, testHeader.bodyLength + ",", testHeader.RemoteSocket.RemoteEndPoint);
        }
コード例 #3
0
        //这里不是必须重写,如果重写了,服务器处理完消息后,会将此消息再发送给客户端,
        //也可以手动调用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);
        }