Esempio n. 1
0
        /// <summary>
        /// Create the thread that will be used to accept
        /// clients while letting the main thread run.
        /// </summary>
        private void CreateAcceptingThread()
        {
            // create a thread that will accept new clients
            Thread thread = new Thread(() => {
                Console.WriteLine("Waiting for connections...");
                while (true)
                {
                    try
                    {
                        // accept a client
                        //Mutex mut = new Mutex();
                        TcpClient client = listener.AcceptTcpClient();
                        Console.WriteLine("Got new connection");

                        // wrap it
                        MyTcpClient c = new MyTcpClient(client);

                        // add it to the view
                        //mut.WaitOne();
                        view.AddClient(c);
                        //  mut.ReleaseMutex();
                    }
                    catch (SocketException)
                    {
                        break;
                    }
                }
                Console.WriteLine("Server stopped");
            });

            // set the thread as the accepter
            accepter = thread;
        }