Пример #1
0
        private void Run()
        {
            try
            {
                TcpListener server = new TcpListener(IPAddress.Any, port);

                server.Start();
                ServerStarted?.Invoke();

                while (!stop)
                {
                    if (server.Pending())
                    {
                        Socket connection = server.AcceptSocket();

                        Worker w = new Worker(this, connection, "Client " + idCounter++);
                        lock (workersLock)
                        {
                            workers.Add(w);
                            UserConnected?.Invoke(w.Username);
                        }
                    }
                }

                server.Server.Close(); // Release all resources
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                ServerClosed?.Invoke();
            }
        }
Пример #2
0
 public static void InvokeServerStarted() => ServerStarted?.Invoke();