Ejemplo n.º 1
0
        public static void StartListening()
        {
            ManualResetEvent waitForConnect = new ManualResetEvent(false);
            ClientService    ClientTask;
            IPAddress        localAddr = IPAddress.Parse("127.0.0.1");
            // Client Connections Pool
            ClientConnectionPool ConnectionPool = new ClientConnectionPool();

            // Client Task to handle client requests
            ClientTask = new ClientService(ConnectionPool);

            ClientTask.Start();

            TcpListener listener = new TcpListener(localAddr, portNum);

            try
            {
                listener.Start();

                int TestingCycle = 3; // Number of testing cycles
                int ClientNbr    = 0;

                // Start listening for connections.
                Console.WriteLine("Waiting for a connection...");
                while (TestingCycle > 0)
                {
                    TcpClient handler = listener.AcceptTcpClient();

                    if (handler != null)
                    {
                        Console.WriteLine("Ecvhange connection#{0} accepted!", ++ClientNbr);

                        // An incoming connection needs to be processed.
                        ConnectionPool.Enqueue(new ClientHandler(handler));

                        //--TestingCycle;
                    }
                    else
                    {
                        break;
                    }
                    waitForConnect.WaitOne(100); //
                }
                listener.Stop();
                Console.WriteLine("Order Listener Stoped");
                // Stop client requests handling
                ClientTask.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }
Ejemplo n.º 2
0
 public ClientService(ClientConnectionPool ConnectionPool)
 {
     this.ConnectionPool = ConnectionPool;
 }