Example #1
0
        static void Main(string[] args)
        {
            bool fileEx = File.Exists(Environment.CurrentDirectory + "\\set.xml");

            if (!fileEx)
            {
                NewSettings();
            }
            else
            {
                bool ok;
                do
                {
                    Console.Write("Обновить настройки (Y/N): ");
                    string answer = Console.ReadLine();
                    if (answer.ToLower() == "y")
                    {
                        ok = true;
                        NewSettings();
                    }
                    else if (answer.ToLower() == "n")
                    {
                        ok = true;
                        Settings.ReadXml();
                    }
                    else
                    {
                        ok = false;
                    }
                } while (!ok);
            }
            limit = NEnter();//задаём параметр N
            //Получаем адрес
            IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(Settings.Fields.ipAddres), Settings.Fields.port);
            //создаем сокет
            Socket listenSocet = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                //Связываем сокет с адресом
                listenSocet.Bind(ipPoint);
                int counter = 0;
                //начинаем прослушивание
                listenSocet.Listen(limit);

                while (true)
                {
                    counter++;

                    Socket client = listenSocet.Accept();

                    //получаем сообщение
                    StringBuilder builder = new StringBuilder();
                    try {
                        //создаем новый поток
                        new Thread(delegate()
                        {
                            //проверяем количество клиентов в очереди
                            if (connection.Count <= limit)
                            {
                                //отправляем информацию об успешном подключении
                                client.Send(CONNECTION_ACCEPTED);
                                connection.Enqueue(new Connection(client, counter));//добавляем клиента в очередь

                                while (connection.Any())
                                {
                                    connection.Peek().RequestFileHandler();//выполняем
                                    var client = connection.Dequeue();
                                    if (!client.Finished)
                                    {
                                        connection.Enqueue(client);
                                    }
                                    ChecUssers();
                                }
                            }
                            else
                            {
                                //отправляем информацию о неудачном подключении
                                client.Send(CONNECTION_REFUSED);
                                //разрываем связь
                                new Connection(client, counter).Abort();
                            }
                        }).Start();
                    }catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        foreach (Connection connection in connection)
                        {
                            Console.WriteLine(((IPEndPoint)client.RemoteEndPoint).Address.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }