Ejemplo n.º 1
0
        public void Test2()
        {
            Console.WriteLine("Test2====================");

            var sRoleLogin1 = new SRoleLogin {
                roleId = 1111, roleName = "张三1111"
            };

            var roleInfo1 = new RoleInfo {
                id = 111, name = "aaa", type = RoleType.type1
            };

            sRoleLogin1.roleInfo = roleInfo1;

            var roleInfo2 = new RoleInfo {
                id = 222, name = "bbb", type = RoleType.type2
            };

            roleInfo2.set.Add(2213);

            sRoleLogin1.roleInfoList.Add(roleInfo2);
            sRoleLogin1.roleInfoList.Add(roleInfo2);
            sRoleLogin1.roleInfoSet.Add(roleInfo2);
            sRoleLogin1.roleInfoSet.Add(roleInfo2);
            sRoleLogin1.roleInfoMap.Add(roleInfo2.id, roleInfo2);

            Console.WriteLine("sRoleLogin1:" + sRoleLogin1);

            var encodedBytes = sRoleLogin1.Encode();

            Console.WriteLine("encodedBytes.Length:{0}", encodedBytes.Length);

            // var fileStream1 = File.Open("D:\\SRoleLogin", FileMode.OpenOrCreate);
            // encodedBytes = sRoleLogin1.Encode();
            // fileStream1.Write(encodedBytes, 0, encodedBytes.Length);
            // fileStream1.Close();

            var fileStream2 = File.Open("D:\\SRoleLogin", FileMode.Open);

            encodedBytes = new byte[fileStream2.Length];
            fileStream2.Read(encodedBytes, 0, encodedBytes.Length);
            Console.WriteLine("fileStream.Length:{0}", fileStream2.Length);

            var sRoleLogin2 = new SRoleLogin();

            sRoleLogin2.Decode(encodedBytes);
            Console.WriteLine("encodedBytes.Length:{0}", encodedBytes.Length);

            Console.WriteLine("sRoleLogin2:" + sRoleLogin2);
        }
Ejemplo n.º 2
0
        public static void Test5()
        {
            Console.WriteLine("Test4====================");

            var sRoleLogin1 = new SRoleLogin {
                roleId = 1111, roleName = "张三1111"
            };

            var roleInfo = new RoleInfo {
                id = 1312, name = "李四1123123", f = 343.435F, d = 4242.54453D
            };

            sRoleLogin1.roleInfo = roleInfo;


            var encodedBytes = sRoleLogin1.Encode();

            var lengthBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(encodedBytes.Length));

            var sendBytes = new byte[encodedBytes.Length + lengthBytes.Length];

            Array.Copy(lengthBytes, sendBytes, lengthBytes.Length);
            Array.Copy(encodedBytes, 0, sendBytes, lengthBytes.Length, encodedBytes.Length);

            var       ip   = IPAddress.Parse("127.0.0.1");
            const int port = 9898;

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(new IPEndPoint(ip, port));

            while (true)
            {
                Thread.Sleep(3000);
                socket.Send(sendBytes);
            }


//            socket.Close();
        }