/// <summary>粘包测试</summary> public static void Test() { var svr = new NetServer { Port = 777, SessionPacket = new PacketFactory { Offset = 0, Size = 0 }, Log = Log.XTrace.Log, //svr.LogSend = true; LogReceive = true }; svr.Received += (s, e) => (s as INetSession).Send(e.Packet); svr.Start(); // 凑齐10个带有长度的数据帧一起发出 var ms = new MemoryStream(); for (var i = 0; i < 5; i++) { var size = i < 4 ? Security.Rand.Next(1400) : Security.Rand.Next(2000, 30000); var str = Security.Rand.NextString(size); var s = str.Substring(0, Math.Min(str.Length, 16)); //var h = str.GetBytes().ToHex(); var mm = new MemoryStream(); mm.WriteArray(str.GetBytes()); var h = mm.ToArray().ToHex(); h = h.Substring(0, Math.Min(h.Length, 32)); Console.WriteLine("{0}\t{1}\t{2}", mm.ToArray().Length, s, h); ms.WriteArray(str.GetBytes()); } var client = new NetUri("tcp://127.0.0.1:777").CreateRemote(); //client.Remote.Address = NetHelper.MyIP(); //client.Remote.Address = System.Net.IPAddress.Parse("1.0.0.13"); client.Packet = new PacketProvider { Offset = 0, Size = 0 }; client.Log = Log.XTrace.Log; client.LogSend = true; //client.LogReceive = true; //client.BufferSize = 1500; client.Received += (s, e) => Log.XTrace.WriteLine("Client {0}", e.Packet.Count); var rs = client.SendAsync(ms.ToArray()).Result; Console.WriteLine("rs={0}", rs.Count); Console.ReadKey(true); client.Close("结束"); svr.Dispose(); }
/// <summary>粘包测试</summary> public static void Test() { var svr = new NetServer(); svr.Port = 777; svr.SessionPacket = new HeaderLengthPacketFactory(); svr.Log = Log.XTrace.Log; svr.LogReceive = true; svr.Start(); // 凑齐10个带有长度的数据帧一起发出 var ms = new MemoryStream(); for (int i = 0; i < 5; i++) { var size = i < 4 ? Security.Rand.Next(1400) : Security.Rand.Next(2000, 30000); var str = Security.Rand.NextString(size); var s = str.Substring(0, Math.Min(str.Length, 16)); //var h = str.GetBytes().ToHex(); var mm = new MemoryStream(); mm.WriteArray(str.GetBytes()); var h = mm.ToArray().ToHex(); h = h.Substring(0, Math.Min(h.Length, 32)); Console.WriteLine("{0}\t{1}\t{2}", mm.ToArray().Length, s, h); ms.WriteArray(str.GetBytes()); } var client = new NetUri("tcp://127.0.0.1:777").CreateRemote(); //client.Remote.Address = NetHelper.MyIP(); //client.Remote.Address = System.Net.IPAddress.Parse("1.0.0.13"); client.Log = Log.XTrace.Log; client.LogSend = true; //client.BufferSize = 1500; client.SendAsync(ms.ToArray()); Console.ReadKey(true); client.Close(); svr.Dispose(); }