Exemple #1
0
        public async void StartListeningIncoming(IPAddress ipaddr = null, int port = 23000)
        {
            if (ipaddr == null)
            {
                ipaddr = IPAddress.Any;
            }
            if (port <= 0)
            {
                port = 23000;
            }

            mIP   = ipaddr;
            mPort = port;

            Debug.WriteLine("IP Address: {0} - Port: {1}", mIP, mPort);

            mTCPListener = new TcpListener(mIP, mPort);
            try
            {
                mTCPListener.Start();

                KeepRunning = true;

                while (KeepRunning)
                {
                    var returnByAccept = await mTCPListener.AcceptTcpClientAsync();

                    mClients.Add(returnByAccept);

                    Debug.WriteLine("Client Connected Successfully, count {0}: {1}", mClients.Count, returnByAccept.ToString());

                    TakeCareofTCPCleint(returnByAccept);

                    ClientEventArgs aClientConnected;
                    aClientConnected = new ClientEventArgs(returnByAccept.Client.RemoteEndPoint.ToString());

                    OnRaiseEvent(aClientConnected);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }