Esempio n. 1
0
        public void testbyNetWork()
        {
            try
            {
                tcpClient client = new tcpClient();

                //建立socket
                client.createSocket();
                //连接服务端
                client.connect("127.0.0.1", 8080);

                TestInfo tiSend = new TestInfo();
                assignTestInfo(ref tiSend);

                //-------------------------encode-----------------------
                UniPacket encodePack = new UniPacket();

                //设置tup版本号,默认是PACKET_TYPE_TUP
                encodePack.ServantName = "TestInfo";
                encodePack.FuncName    = "TestInfo";

                short  tests   = -100;
                int    testi   = int.MaxValue;
                long   testl   = -1000;
                float  testf   = -100.032f;
                string teststr = "teststring";

                encodePack.Put("tests", tests);
                encodePack.Put("testi", testi);
                encodePack.Put("testl", testl);
                encodePack.Put("testf", testf);
                encodePack.Put("teststr", teststr);
                encodePack.Put <TestInfo>("TestInfo", tiSend);

                byte[] bufferS = encodePack.Encode();

                Console.WriteLine("打印发送 buffer");
                int ret = client.send(bufferS);

                StringBuilder sb = new StringBuilder();
                tiSend.Display(sb, 0);
                Console.WriteLine(sb.ToString());
                Console.WriteLine("发送大小: " + ret);

                byte[] bufferR = new byte[8192];

                ret = client.receive(bufferR);

                Console.WriteLine("打印接收 buffer");
                Console.WriteLine("接收大小:" + ret);

                //----------------------decode----------------------------
                if (ret > sizeof(int))
                {
                    UniPacket decodePack = new UniPacket();
                    decodePack.Decode(bufferR);

                    TestInfo tiRecv   = new TestInfo();
                    short    dtests   = 0;
                    int      dtesti   = 0;
                    long     dtestl   = 0;
                    float    dtestf   = 0.0f;
                    string   dteststr = "";

                    //使用新的Get<T>(string Name, T DefaultObj)
                    dtests   = decodePack.Get <short>("tests", dtests);
                    dtesti   = decodePack.Get <int>("testi", dtesti);
                    dtestl   = decodePack.Get <long>("testl", dtestl);
                    dtestf   = decodePack.Get <float>("testf", dtestf);
                    dteststr = decodePack.Get <string>("teststr", dteststr);
                    tiRecv   = decodePack.Get <TestInfo>("TestInfo", tiRecv);

                    sb.Clear();
                    tiRecv.Display(sb, 0);
                    Console.WriteLine("end:" + sb.ToString());
                    Console.WriteLine("dtests:" + dtests);
                    Console.WriteLine("dtesti:" + dtesti);
                    Console.WriteLine("dtestl:" + dtestl);
                    Console.WriteLine("dtestf:" + dtestf);
                    Console.WriteLine("dteststr:" + dteststr);
                    Console.ReadLine();
                }

                client.close();
            }
            catch (Exception e)
            {
                Console.WriteLine("testbyNetWork: " + e.Message);
            }
        }