static void Test6() { // UDP没有客户端服务器之分。推荐使用NetUri指定服务器地址 var udp = new UdpServer(); udp.Remote = new NetUri("udp://smart.peacemoon.cn:7"); udp.Received += (s, e) => { XTrace.WriteLine("收到:{0}", e.ToStr()); }; udp.Open(); udp.Send("新生命团队"); udp.Send("学无先后达者为师!"); // Tcp客户端会话。改用传统方式指定服务器地址 var tcp = new TcpSession(); tcp.Remote.Host = "smart.peacemoon.cn"; tcp.Remote.Port = 13; tcp.Open(); var str = tcp.ReceiveString(); XTrace.WriteLine(str); // 产品级客户端用法。直接根据NetUri创建相应客户端 var client = new NetUri("tcp://smart.peacemoon.cn:17").CreateRemote(); client.Received += (s, e) => { XTrace.WriteLine("收到:{0}", e.ToStr()); }; client.Open(); Thread.Sleep(1000); }
// 如果是外部传进来的Socket,也销毁,就麻烦大了 ///// <summary>子类重载实现资源释放逻辑时必须首先调用基类方法</summary> ///// <param name="disposing">从Dispose调用(释放所有资源)还是析构函数调用(释放非托管资源)</param> //protected override void OnDispose(bool disposing) //{ // base.OnDispose(disposing); // if (_Socket != null) // { // _Socket.Dispose(); // _Socket = null; // } // if (_Socket2 != null) // { // _Socket2.Dispose(); // _Socket2 = null; // } //} #endregion #region 方法 void EnsureSocket() { if (_Socket == null) { //var client = NetService.Container.Resolve<ISocketClient>(ProtocolType); //client.Port = Port; var client = new NetUri(ProtocolType, "", Port).CreateClient(); client.Open(); client.Client.SendTimeout = Timeout; client.Client.ReceiveTimeout = Timeout; _Socket = client; } }
static async void Test5() { Console.WriteLine("服务端1,客户端2:"); if (Console.ReadKey().KeyChar == '1') { var svr = new NetServer(777); #if DEBUG svr.Log = XTrace.Log; svr.LogSend = true; svr.LogReceive = true; #endif //svr.Add<DefaultCodec>(); svr.Add(new LengthFieldCodec { Size = 4 }); //svr.Add<BinaryCodec<UserY>>(); svr.Add <JsonCodec <UserY> >(); svr.Add <EchoHandler>(); svr.Start(); } else { var client = new NetUri("tcp://127.0.0.1:777").CreateRemote(); #if DEBUG client.Log = XTrace.Log; client.LogSend = true; client.LogReceive = true; #endif //client.Add<DefaultCodec>(); client.Add(new LengthFieldCodec { Size = 4 }); //client.Add<BinaryCodec<UserY>>(); client.Add <JsonCodec <UserY> >(); client.Open(); //client.Send("Stone"); var user = new UserY { ID = 0x1234, Name = "Stone", DisplayName = "大石头" }; for (var i = 0; i < 3; i++) { var rs = await client.SendAsync(user) as UserY; XTrace.WriteLine("{0} {1}", rs.Name, rs.DisplayName); } } }
// 如果是外部传进来的Socket,也销毁,就麻烦大了 ///// <summary>子类重载实现资源释放逻辑时必须首先调用基类方法</summary> ///// <param name="disposing">从Dispose调用(释放所有资源)还是析构函数调用(释放非托管资源)</param> //protected override void OnDispose(bool disposing) //{ // base.OnDispose(disposing); // if (_Socket != null) // { // _Socket.Dispose(); // _Socket = null; // } // if (_Socket2 != null) // { // _Socket2.Dispose(); // _Socket2 = null; // } //} #endregion #region 方法 void EnsureSocket() { if (_Socket == null) { //var client = NetService.Container.Resolve<ISocketClient>(ProtocolType); //client.Port = Port; var client = new NetUri(ProtocolType, "", Port).CreateClient(); client.Open(); client.Client.SendTimeout = Timeout; client.Client.ReceiveTimeout = Timeout; _Socket = client; } }