Chinaunix首页 | 论坛 | 博客
  • 博客访问: 490832
  • 博文数量: 33
  • 博客积分: 4168
  • 博客等级: 上校
  • 技术积分: 675
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-26 20:21
文章分类

全部博文(33)

文章存档

2013年(8)

2012年(2)

2011年(7)

2010年(1)

2009年(4)

2008年(11)

分类:

2009-03-27 11:29:30

Midapex.net 是一个很好的开源网络通讯开发库,首先感谢作者的辛勤劳动。这里是源地址:
 
 
其中有个疑问:原开发文档中有句话“。。。指定远程服务器的地址和端口后,调用Start方法,开始连接服务器,ClientIsRun属性变为True,当连接到服务器后IsConnected变为True,如果连接失败,IsRunIsConnected都变化为false,并报告错误信息。如果客户端处于IsRun=trueIsConnected=false状态,说明客户端正在连接。。。”,实测时好像连接失败后的2个状态属性不对。
 
 
最近教学软件项目的需要,在原有代码基础上增加了一些事件处理,主要目的是为了适合在主线程中使用,请原作者原谅。
 

因许多方法都是通过重载完成,不方便在实现重载的类中使用,因为在这个类中的重载方法中很多情况下都必须与主线程类交互)。具体增加功能和修改位置列表如下:

事件

事件名

位置

引用示例

会话创建后

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-3Example-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 Main(string[] args)

        {

            Trace.Listeners.Add(new ConsoleTraceListener());

            dd = new CommandServer<CommandSession>();

            dd.OnDataIn += new EventHandlerTEventArgs<string>>(dd_OnDataIn);

            dd.OnSessionCreate += new EventHandlerTEventArgs<string>>(dd_OnSessionCreate);

            dd.OnSessionClose += new EventHandlerTEventArgs<string>>(dd_OnSessionClose);

            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 Main(string[] args)

        {

            Trace.Listeners.Add(new ConsoleTraceListener());

            dd = new CommandClient<CommandSession>();

            dd.OnDataIn += new EventHandlerTEventArgs<string>>(dd_OnDataIn);

            dd.OnConnectSuccess += new EventHandlerTEventArgs<string>>(dd_OnConnectSuccess);

            dd.OnConnectFail += new EventHandlerTEventArgs<string>>(dd_OnConnectFail);

            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 EventHandlerTEventArgs<MessageBlock>>(dd_OnDataIn);

void dd_OnDataIn(object sender, Midapex.net.TEventArgs<MessageBlock> e)

{

      MessageBlockSession session = (MessageBlockSession)sender;

      Console.WriteLine("Received MessageBlock=" + e.Param.Body.ToString());

}

 

 Example-4

服务器端

class Program

{

        static MessageBlockServer<MessageBlockSession> dd = null;

        static void Main(string[] args)

        {

            dd = new MessageBlockServer<MessageBlockSession>();

            dd.OnSessionCreate += new EventHandlerTEventArgs<string>>(dd_OnSessionCreate);

            dd.OnSessionClose += new EventHandlerTEventArgs<string>>(dd_OnSessionClose);

            dd.OnDataIn += new EventHandlerTEventArgs<MessageBlock>>(dd_OnDataIn);

            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 Main(string[] args)

        {

            dd = new MessageBlockClient<MessageBlockSession>();

            dd.OnConnectSuccess += new EventHandlerTEventArgs<string>>(dd_OnConnectSuccess);

            dd.OnConnectFail += new EventHandlerTEventArgs<string>>(dd_OnConnectFail);

            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();

        }

}

 

阅读(2424) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-04-08 19:20:10

Midapex.net.TEventArgs 这个方法是什么?有代码吗?