public void testtupprotocol() { try { tcpClient client = new tcpClient(); //建立socket client.createSocket(); //连接服务端 client.connect("127.0.0.1", 8080); //-------------------------encode----------------------- TarsUniPacket encodePack = new TarsUniPacket(); encodePack.RequestId = 1; //设置tup版本号,默认是PACKET_TYPE_TUP encodePack.setTarsVersion(Const.PACKET_TYPE_TUP3); encodePack.setTarsPacketType(Const.PACKET_TYPE_TARSNORMAL); encodePack.ServantName = "Test.HelloServer.HelloImpObj"; encodePack.FuncName = "testHello"; string sHello = "i am a string from csharp"; encodePack.Put("s", sHello); byte[] bufferS = encodePack.Encode(); Console.WriteLine("打印发送 buffer"); int ret = client.send(bufferS); Console.WriteLine(bin2hex(bufferS)); Console.WriteLine("发送大小: " + ret); byte[] bufferR = new byte[8192]; ret = client.receive(bufferR); //----------------------decode---------------------------- if (ret > sizeof(int)) { BinaryReader br = new BinaryReader(new MemoryStream(bufferR)); //前4个字节为tup包大小 int len = ByteConverter.ReverseEndian(br.ReadInt32()); byte[] bufferD = new byte[len]; System.Array.Copy(bufferR, 0, bufferD, 0, len); TarsUniPacket respPack = new TarsUniPacket(); respPack.Decode(bufferD); Console.WriteLine("打印接收 buffer"); Console.WriteLine(bin2hex(bufferD)); Console.WriteLine("接收大小:" + ret); if (respPack.getTarsResultCode() == 0) { string sOut = ""; sOut = respPack.Get <string>("r", sOut); Console.WriteLine("sOut:" + sOut); } else { Console.WriteLine("tup invoke failure"); } Console.ReadLine(); } client.close(); } catch (Exception e) { Console.WriteLine("testbyNetWork: " + e.Message); } }
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); } }