예제 #1
0
        public void Start_Listen(string ipAddress, int portNum)
        {
            if (FileBaseDirectory.DlcDir == null || FileBaseDirectory.UserDir == null)
            {
                Console.WriteLine("Please specify file path");
            }

            if (Directory.Exists(FileBaseDirectory.DlcDir) == false)
            {
                Console.WriteLine("No directory on DlcPath");
                return;
            }

            if (Directory.Exists(FileBaseDirectory.UserDir) == false)
            {
                Console.WriteLine("No directory on UserPath");
            }

            _listen = new Listener(_communicatorPool);
            if (ipAddress != null)
            {
                _listen.Host = ipAddress;
            }
            if (portNum > 1023)
            {
                _listen.Port = portNum;
            }
            _listen.CallBackonNewclient += Newclient;//add Newclient callback handler to delegate
            //this delegate is called when new client is accepted
            _listen.ActivateAcception = true;
            _listen.Start(_backlog);
            while (true)
            {
                //              Thread.Sleep(1500);
                Console.ReadLine();
                Console.WriteLine("Communicator Stack size:" + _communicatorPool.StackSize());
            }
        }
예제 #2
0
        public void Threaded_accept()//this function accepts client. It's executed on new thread
        {
            while (ActivateAcception)
            {
                if (_communicatorPool.StackSize() > 10)
                {
                    Console.WriteLine("Accepting");
                    _acceptArgs.AcceptSocket = null;                       // Set to null for receiving new client socket
                    bool pending = _listenSocket.AcceptAsync(_acceptArgs); // If completed Asynchronously
                    //On_Accept_Completed is called Automatically

                    if (pending == false) // If AcceptAsync was completed synchronously
                    {
                        _pendingThread = new Thread(StartNewThread);
                        _pendingThread.Start(); //This is for keep receiving requests while Thread is working
                    }

                    _flowControlEvent.WaitOne(); //wait until scoket is accepted
                }
            }
        }