Exemplo n.º 1
0
        private void TcpListenerMethod()
        {
            TcpListener _TcpListener = new TcpListener(IPAddress.Any, 8542);

            _TcpListener.Start();
            while (_doaccept)
            {
                TcpClient tcpClient = _TcpListener.AcceptTcpClient();

                bool allowed = false;

                lock (this)
                {
                    if (_doaccept)
                    {
                        string ip = tcpClient.Client.RemoteEndPoint.ToString();
                        ip = ip.Substring(0, ip.IndexOf(':'));

                        //todo: check connection requests per IP
                        allowed = (_clientHandler.GetIPConnectionsCount(ip) < 10);

                        if (allowed)
                        {
                            ClientConnection cc = new ClientConnection(tcpClient);
                            cc.IP = ip;
                            _clientHandler.AddClientConnection(cc);
                        }
                    }
                }

                if (!allowed)
                {
                    try
                    {
                        tcpClient.Close();
                    }
                    catch
                    {
                    }
                }
            }
        }