예제 #1
0
        /// <summary>
        /// 已重载。
        /// </summary>
        protected override void EnsureCreateServer()
        {
            Name = "文件服务";

            TcpServer svr = new TcpServer(Address, Port);
            svr.Accepted += new EventHandler<NetEventArgs>(server_Accepted);
            // 允许同时处理多个数据包
            svr.NoDelay = false;
            // 使用线程池来处理事件
            svr.UseThreadPool = true;
        }
예제 #2
0
        /// <summary>
        /// 开始测试
        /// </summary>
        public static void Test()
        {
            //MaxTest();
            //return;

            Int32 port = 7;
            TcpServer server = new TcpServer(port);
            server.NoDelay = false;
            server.UseThreadPool = true;
            server.Accepted += new EventHandler<NetEventArgs>(server_Accepted);
            server.Error += delegate(Object sender, NetEventArgs e)
            {
                if (e.SocketError != SocketError.Success)
                    XTrace.WriteLine("服务端 {0}错误 {1}", e.RemoteEndPoint, e.SocketError.ToString());
                else
                    XTrace.WriteLine("服务端 {0}断开!", e.RemoteEndPoint);
            };
            server.Start();
            Console.WriteLine("任意键开始……");
            ThreadPool.QueueUserWorkItem(delegate
            {
                while (true)
                {
                    Console.WriteLine("Pool[Create={0} Stock={1} NotStock={2} Sessions={3}]", TcpServer.Pool.CreateCount, TcpServer.Pool.StockCount, TcpServer.Pool.NotStockCount, server.Sessions.Count);

                    Thread.Sleep(1000);
                }
            });
            Console.ReadKey(true);

            MaxTest();

            //String msg = "测试数据!";
            //Byte[] data = Encoding.UTF8.GetBytes(msg);
            //IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0);
            //List<SocketClient> list = new List<SocketClient>();
            //for (int n = 0; n < 100; n++)
            //{
            //    TcpClientEx client = new TcpClientEx();
            //    list.Add(client);
            //    SetEvent(client, false);
            //    client.Connect(IPAddress.Loopback, port);
            //    //client.Connect("192.168.1.101", port);
            //    //client.Connect("192.168.0.100", port);
            //    client.ReceiveAsync();

            //    client.Send("Hello");
            //    //client.Send("我来啦!");
            //    //for (int i = 0; i < 10; i++)
            //    //{
            //    //    client.Send((i + 1).ToString());
            //    //String str = client.ReceiveString();
            //    //Console.WriteLine("客户端 收到{0}数据 {1}", client.RemoteEndPoint, str);
            //    //}
            //    //Thread.Sleep(1000);
            //    //client.Close();
            //}

            //Console.WriteLine("任意键结束客户端!");
            //Console.ReadKey(true);
            //foreach (SocketClient item in list)
            //{
            //    item.Close();
            //}

            Console.WriteLine("任意键停止服务…… {0}", server.Sessions.Count);
            Console.ReadKey(true);
            server.Close();
        }