Example #1
0
        public static void Main(string[] args)
        {
            try
            {
                IPEndPoint endPoint = null;

                if (args.Length == 5)
                {
                    endPoint = new IPEndPoint(IPAddress.Parse(args[3]), int.Parse(args[4]));
                }

                else if (args.Length != 3)
                {
                    Console.WriteLine("Usage: TreeChat.exe {username} {losspercent} {port} [optional: {parentip} {parentport}]");
                    return;
                }

                TreeChat chat = new TreeChat(userName: args[0], lossPercent: int.Parse(args[1]),
                                             parentNode: endPoint, localPort: int.Parse(args[2]));

                chat.Launch();

                // Process.GetCurrentProcess().Exited += (sender, eventArgs) => chat.InformAboutExit();

                string text;
                while ((text = Console.ReadLine()) != @"/quit")
                {
                    chat.SendMessage(text);
                }

                chat.Stop();
            }

            catch (Exception e)
            {
                Console.WriteLine($"[Error]: Something went wrong: {e.Message}");
            }
        }
Example #2
0
        public static void Main(string[] args)
        {
            IPEndPoint endPoint;

            if (args.Length == 2)
            {
                endPoint = null;
            }
            else if (args.Length == 4)
            {
                endPoint = new IPEndPoint(IPAddress.Parse(args[2]), int.Parse(args[3]));
            }
            else
            {
                Console.WriteLine("Invalid arguments");
                return;
            }

            TreeChat chat = new TreeChat(args[0], 0, endPoint, int.Parse(args[1]));

            chat.Start();

            AppDomain.CurrentDomain.DomainUnload += (sender, eventArgs) => chat.BroadcastDead().Wait();

            while (true)
            {
                string messageText = Console.ReadLine();
                if (messageText?.ToLower() == "quit")
                {
                    chat.Stop();
                    return;
                }

                chat.SendMessage(messageText);
            }
        }