Пример #1
0
        static void Main(string[] args)
        {
            Server     server     = new Server(null, 2222, "xs8\n");        // '\n' used if the client is sending data from a terminal which appends a new line at the end of the data
            Connection connection = new Connection(server);

            Connection.Client client = new Connection.Client(connection);


Start:
            if (connection.Status == Connection.StatusFlag.NotActive)
            {
                connection.StartAsync();

                Console.WriteLine("[Server created]\r\n");
            }

            Console.WriteLine(" > Waiting client...");

            while (client.Status == Connection.Client.StatusFlag.UnknownOrDisconnected)
            {
                Thread.Sleep(10);
            }

            Console.WriteLine($"  > Client connected : {{{client.IP}:{client.Port}}}");



            if (client.Status == Connection.Client.StatusFlag.AuthNoPassword)
            {
                client.WriteLine("\r\n[Successfully connected to the server!]");


                read(server, connection, client);
                write(server, connection, client);
            }
            else if (server.AuthRequired)
            {
                do
                {
                    if (!client.IsAuth && client.Status == Connection.Client.StatusFlag.Connected)
                    {
                        client.AskAuth("\r\n[Connected to the server]\r\n > Please insert the password\r\n");
                    }
                    else if (client.Status == Connection.Client.StatusFlag.AuthWrongPassword)
                    {
                        client.AskAuth("\r\n[Wrong password!]\r\n > Please insert the password\r\n");
                    }
                } while (!client.IsAuth && client.Status != Connection.Client.StatusFlag.AuthTimeout && client.Status != Connection.Client.StatusFlag.UnknownOrDisconnected);


                if (client.Status == Connection.Client.StatusFlag.AuthPassword)
                {
                    Console.WriteLine("\r\n[Auth Success]\r\n");
                    client.WriteLine("\r\n[Auth Success]\r\n");

                    read(server, connection, client);
                    write(server, connection, client);
                }
                else if (client.Status == Connection.Client.StatusFlag.AuthTimeout)
                {
                    client.WriteLine("\r\n[Disconnected from the server because of timeout]\r\n");
                    Console.WriteLine("\r\n[Auth failed! - Timeout]\r\n");

                    client.Disconnect();

                    goto Start;
                }
            }

            // We block the main thread while the client is successfully connected
            while (client.Connected)
            {
                Thread.Sleep(100);
            }


            if (client.Status == Connection.Client.StatusFlag.UnknownOrDisconnected)
            {
                Console.WriteLine("\r\n[Client disconnected!]\r\n");

                goto Start;
            }
        }