コード例 #1
0
        /// <summary>
        /// a delegate to
        /// </summary>
        private void Listen()
        {
            //construct a listener
            listener = new TcpListener(IPAddress.Any, port);
            //backlog是最大允许的同时排队的连接数,用于处理并发的连接,与实际的连接数无关,因此该数不必太大
            listener.Start(512);

            while (isRunning)
            {
                try
                {
                    if (!listener.Pending())
                    {
                        Thread.Sleep(1);
                        continue;
                    }

                    //accept the pending connection
                    Socket client = listener.AcceptSocket();
                    // TcpClient client = listener.AcceptTcpClient();

                    if (action != null)
                    {
                        ThreadPoolHelper.ExecThreadPool(action, client);
                    }

                    //client.Shutdown(SocketShutdown.Both);
                    //client.Close();
                    //RequestHandle requestHandle = new RequestHandle(client);
                    //ThreadPoolHelper.ExecThreadPool(ThreadProc, requestHandle);
                    Thread.Sleep(1);
                }
                catch (Exception ex)
                {
                    _lastEx = ex;
                }
            }
        }