A Socket/SerialPort App Server.

最新更新:

2011-09-09 修改AppSession实现重载ToString方法用于输出Seesion标识(TCP/UDP为远程终结点,SerialPort为COM端口号)

 

1、串口服务器测试

 

        /// <summary>
        /// 测试串口服务器
        /// </summary>
        static void TestSerialPort()
        {
            IAppServer server = new SerialPortServer();
            server.Received += new EventHandler<ReceivedArgs>((sender, args) =>
            {
                Console.WriteLine("收到来自{0}的消息:{1}",
                    args.Session.UserToken,
                    Encoding.Default.GetString(args.Bytes));
                Console.WriteLine("接收回复结果{0}", args.Session.Send(new DefaultCommand(args.Bytes)));
            });
            server.Setup(new SerialPortConfig(5, 9600));
            if (!server.Start())
            {
                Console.WriteLine("串口服务器创建失败!");
            }
            else
            {
                IAppSession session = server.CreateSession();
                for (int i = 0; i < 10; i++)
                {
                    session.Send(new DefaultCommand(Encoding.Default.GetBytes(string.Format("第{0}次发送测试消息!", i))));
                }
            }
        }

 

2、网络服务器测试

 

         //测试Socket服务器
            IAppServer server =  new SocketServer();
            server.RegistSession += new EventHandler<AppSessionArgs>((sender, args) =>
            {
                object show = args.Session.UserToken;
                if (args.Session.UserToken is SocketAsyncEventArgs)
                {
                    show = (args.Session.UserToken as SocketAsyncEventArgs).AcceptSocket.RemoteEndPoint;
                }
                Console.WriteLine("注册AppSession:{0}", 
                    show);
            });
            server.UnRegistSession += new EventHandler<AppSessionArgs>((sender, args) =>
            {
                object show = args.Session.UserToken;
                if (args.Session.UserToken is SocketAsyncEventArgs)
                {
                }
                Console.WriteLine("注销AppSession:{0}",
                    show);
            });
            server.Received += new EventHandler<ReceivedArgs>((sender, args) =>
            {
                object show = args.Session.UserToken;
                if (args.Session.UserToken is SocketAsyncEventArgs)
                {
                    show = (args.Session.UserToken as SocketAsyncEventArgs).AcceptSocket.RemoteEndPoint;
                }
                Console.WriteLine("收到来自{0}的消息:{1}",
                    show,
                    Encoding.Default.GetString(args.Bytes));
                Console.WriteLine("接收回复结果:{0}",args.Session.Send(new DefaultCommand(args.Bytes)));
            });

            //过滤列表{TCP模式允许过滤连接客户端,UDP模式允许过滤接收端}
            IList<EndPoint> filterList = new List<EndPoint>()
            {
                new IPEndPoint(IPAddress.Parse("192.168.0.42"),6666),
                new IPEndPoint(IPAddress.Parse("192.168.0.42"),9999),
            };
            //安装配置为UDP服务器
            //server.Setup(new SocketConfig(new IPEndPoint(IPAddress.Any, 5005),1024, new EndPointFilter(filterList)));
           
             //安装配置为TCP服务器
            server.Setup(new SocketConfig(new IPEndPoint(IPAddress.Any, 5005),
                10,
                1024,
                new SocketConfig.KeepLiveArgs(3000),
                null/*new EndPointFilter(filterList)*/));

            if (server.Start())
            {
                //进行一些Socket测试
            }

Last edited Sep 14, 2011 at 3:00 PM by huliang, version 9