分类:
2009-03-27 11:29:30
因许多方法都是通过重载完成,不方便在实现重载的类中使用,因为在这个类中的重载方法中很多情况下都必须与主线程类交互)。具体增加功能和修改位置列表如下:
事件 |
事件名 |
位置 |
引用示例 |
会话创建后 |
OnSessionCreate |
TcpServerBase |
Example-1 |
会话关闭前 |
OnSessionClose |
TcpServerBase |
Example-1 |
连接服务器成功后 |
OnConnectSuccess |
TcpClientBase |
Example-2 |
连接服务器失败时 |
OnConnectFail |
TcpClientBase |
Example-2 |
收到客户端命令后 |
OnDataIn |
CommandServer |
Example-1 |
收到服务器命令后 |
OnDataIn |
CommandClient |
Example-2 |
收到客户端消息后 |
OnDataIn |
MessageBlockServer |
Example-3,Example-4 |
收到服务器消息后 |
OnDataIn |
MessageBlockClient |
Example-4 |
服务端掉线后 |
OnOffLine |
TcpClientBase |
Example-4 |
注: 服务端捕获客户端掉线可利用 TcpServerBase.OnSessionClose 事件。 |
另外实现了DataBlock类的一个重载方法:ToString。用法: MessageBlock x à x.Param.Body.ToString() 得到消息块的实际字符。
初学 C#, 修改不当之处请读者指正。
Example-1
class Program { static CommandServer<CommandSession> dd = null; static void { Trace.Listeners.Add(new ConsoleTraceListener()); dd = new CommandServer<CommandSession>(); dd.OnDataIn += new EventHandler dd.OnSessionCreate += new EventHandler dd.OnSessionClose += new EventHandler dd.NewLines = new string[] { "\r\n" }; dd.Encoding = Encoding.UTF8; dd.Port = 10001; dd.Start(); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); dd.Stop(); } static void dd_OnSessionClose(object sender, Midapex.net.TEventArgs<string> e) { CommandSession session = (CommandSession)sender; string nul = e.Param; } static void dd_OnSessionCreate(object sender, Midapex.net.TEventArgs<string> e) { CommandSession session = (CommandSession)sender; string nul = e.Param; } public static void dd_OnDataIn(object sender, Midapex.net.TEventArgs<string> e) { CommandSession session = (CommandSession)sender; string command = e.Param; NetDebuger.PrintDebugMessage(session, string.Format("Revc CMD:{0}", command)); //把收到的命令返回到客户端 dd.Send(session, command + dd.NewLines[0]); } } |
Example-2
class Program { static CommandClient<CommandSession> dd = null; static void { Trace.Listeners.Add(new ConsoleTraceListener()); dd = new CommandClient<CommandSession>(); dd.OnDataIn += new EventHandler dd.OnConnectSuccess += new EventHandler dd.OnConnectFail += new EventHandler dd.NewLines = new string[] { "\r\n" }; dd.Encoding = Encoding.UTF8; dd.Host = "192.168.0.100"; dd.Port = 10001; dd.Start(); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); dd.Stop(); } static void dd_OnConnectFail(object sender, Midapex.net.TEventArgs<string> e) { string xx = e.Param.ToString(); } static void dd_OnConnectSuccess(object sender, Midapex.net.TEventArgs<string> e) { CommandSession session = (CommandSession)sender; session.Close(); } static void dd_OnDataIn(object sender, Midapex.net.TEventArgs<string> e) { CommandSession session = (CommandSession)sender; string command = e.Param; NetDebuger.PrintDebugMessage(session, string.Format("Revc CMD:{0}", command)); //把收到的命令返回 dd.Send(command + dd.NewLines[0]); } } |
Example-3
… MessageBlockServer<MessageBlockSession> dd = new MessageBlockServer<MessageBlockSession>(); … dd.OnDataIn += new EventHandler … void dd_OnDataIn(object sender, Midapex.net.TEventArgs<MessageBlock> e) { MessageBlockSession session = (MessageBlockSession)sender; Console.WriteLine("Received MessageBlock=" + e.Param.Body.ToString()); } |
服务器端 |
class Program { static MessageBlockServer<MessageBlockSession> dd = null; static void { dd = new MessageBlockServer<MessageBlockSession>(); dd.OnSessionCreate += new EventHandler dd.OnSessionClose += new EventHandler dd.OnDataIn += new EventHandler dd.Port = 10005; dd.HeartBeatPeriod = 10000;//10 second dd.Start(); Console.WriteLine("Press enter to exit..."); Console.ReadLine(); dd.Stop(); } static void dd_OnDataIn(object sender, Midapex.net.TEventArgs<MessageBlock> e) { MessageBlockSession session = (MessageBlockSession)sender; Console.WriteLine("收到客户发来的消息 = " + e.Param.Body.ToString()); } static void dd_OnSessionClose(object sender, Midapex.net.TEventArgs<string> e) { MessageBlockSession session = (MessageBlockSession)sender; Console.WriteLine("客户端发出关闭连接。 "); } static void dd_OnSessionCreate(object sender, Midapex.net.TEventArgs<string> e) { MessageBlockSession session = (MessageBlockSession)sender; //向客户端发送消息 string data = " Hi,我是服务!"; dd.Send(session, new MessageBlock(Encoding.Unicode.GetBytes(data))); } } |
客户端 |
class Program { static MessageBlockClient<MessageBlockSession> dd = null; static void { dd = new MessageBlockClient<MessageBlockSession>(); dd.OnConnectSuccess += new EventHandler dd.OnConnectFail += new EventHandler dd.OnDataIn += new EventHandler<MessageBlockArgs>(dd_OnDataIn); dd.OnOffLine += new EventHandler<Midapex.net.TEventArgs<string>>(dd_OnOffLine); dd.Host = "127.0.0.1"; dd.Port = 10005; dd.HeartBeatPeriod = 10000; //10 second dd.Start(); // 向服务器发送消息 string data = "Hi,我是客户!"; dd.Send(new MessageBlock(Encoding.Unicode.GetBytes(data))); Console.WriteLine("Press enter to exit..."); } static void dd_OnDataIn(object sender, MessageBlockArgs e) { MessageBlockSession session = (MessageBlockSession)sender; Console.WriteLine("服务器发来数据 = " + e.MessageBlock.Body.ToString()); } static void dd_OnConnectFail(object sender, Midapex.net.TEventArgs<string> e) { string xx = e.Param.ToString(); Console.WriteLine(xx); } static void dd_OnConnectSuccess(object sender, Midapex.net.TEventArgs<string> e) { MessageBlockSession session = (MessageBlockSession)sender; } static void dd_OnOffLine(object sender, Midapex.net.TEventArgs<string> e) { MessageBlockSession session = (MessageBlockSession)sender; Console.WriteLine("服务器掉线。"); session.Close(); } } |